Tag: mongodb

使用replaceOne时,节点upsertedId为null

我使用replaceOne作为便捷方法更新文档或创build一个文档,如果它不存在。 问题是,在创build的情况下,我没有得到upsertedId 。 但是我需要创build的文档。 的package.json "mongodb": "^2.2.31" MongoDb服务器版本是3.4.6 this.collection.replaceOne(filter,item,{upsert:true,w:1})返回 { modifiedCount: 0, matchedCount: 0, result: { "n": 1, "nModified": 0, "upserted": [ { "index": 0, "_id": null } ], "ok": 1 }, upsertedCount: 1 upsertedId: { "index": 0, "_id": null } } 在数据库中,upsertedObject也是使用null _id创build的

任何人都可以解释两个用户在mongodb3.4.5中的区别

//the first user { "_id" : "admin.guguji55", "user" : "guguji55", "db" : "admin", "roles" : [ { "role" : "readWrite", "db" : "test" } ] } //the second user { "_id" : "test.guguji66", "user" : "guguji66", "db" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" } ] } 我创build了两个用户,'guguji55'不能连接我的testing数据库,但'guguji66'可以做到这一点。我不知道为什么。我的nodejs连接代码如下: MongoClient.connect('mongodb://guguji66:du****kuan****@localhost:27017/test', function(err, […]

Mongodb身份validation错误Ubuntu

我正在为parsing服务器设置mongodb。 MongoDB shell版本:3.2.12 以下是我用来设置mongodb的步骤。 mongo > use mydbname > db.createUser({ user: "mydbuser", pwd: "mydbpwd", roles: [ { role: "admin", db: "mydbname" } ] }) 导入数据库 mongorestore ~/backup/mydbname –db=mydbname –username=mydbuser –password=mydbpwd 运行NPM npm install npm start 直到这里它工作绝对好。 但过了一段时间或1天后,它会抛出错误:身份validation失败。 错误 请帮助解决这个问题。

MongoDB:如何通过连接获取数据

我在MongoDB中有两个模式 var userSchema = new Schema({ first_name: String, last_name: String, email: String }); var messageSchema = new Schema({ sender: {type: Schema.Types.ObjectId, ref: 'users' }, receiver: {type: Schema.Types.ObjectId, ref: 'users' }, message: String, sentOn: Date, }); var Users = mongoose.model('users', userSchema); var Messages = mongoose.model('messages', messageSchema); 我如何查询从“用户”集合中获取所有用户,并从“消息”集合中获取最后一条消息(单个)?

是否有可能在MongoDB中“join”文档并删除“父”文档,但仍保留“子”文档?

我正在用MEAN堆栈构build一个CRUD应用程序。 我有一个patients模式,我需要给这些患者添加一个medical history ,我想知道是否可以像使用mysql中的“连接”来使用这些文档。 如果是的话,甚至在病人被删除之后,我怎么能保留medical history ? 这是我patient模式 var mongoose = require("mongoose"); var Schema = mongoose.Schema; var paciente_schema = new Schema({ nombre: {type: String, required: true}, estado: {type: String, required: true, default: "En espera"}, fecha: {type: String, required: true}, edad: {type: String, required: true}, sexo: {type: String, required: true}, direccion: {type: String, required: true}, contacto: […]

如何dockerize我的nodejs快递应用程序托pipe在亚马逊linux ami?

1.my上面的应用程序技术栈是expressjs,nodejs,mongoDB,redisDB,s3(存储)。 2.API托pipe在Linux AMI上3.我需要为我的应用程序创builddocker容器镜像。

使用mongoose有效地查询索引子数组

我试图在MongoDB中存储每个用户的卡片集合。 collections基本上是一系列拥有相关数量的自有卡。 Collection { user: …, cards: [ {cardId: 133, qty: 3}, {cardId: 22, qty: 1} ] } 我正在用node.js和[ {cardId: 133, qty: 2}, {cardId: 111, qty: 4} ]构build一个API,以[ {cardId: 133, qty: 2}, {cardId: 111, qty: 4} ]的forms接收请求。 现在,我需要在cards数组中创build卡片条目(如果它不存在),或者在数量已经存在的情况下更新数量。 我需要这样做,因为集合可能包含数以千计的卡,所以我想出了这个Schema : var OwnedCard = new Schema({ cardId: { type: String, index: true, required: true}, qty: { […]

数据传输对象(DTO)是否用Node.js / Express / MongoDB构buildREST API?

我是Node.js的新手,我是后端开发新手,所以这可能是一个愚蠢的问题。 比方说,我有这样的模型: var mongoose = require('mongoose'); var UserSchema = new mongoose.Schema({ name: String, birthDate: Date, email: String, profileImageUrl: String }); mongoose.model('User', UserSchema); module.exports = mongoose.model('User'); 我有一个userController函数来获取所有这样的用户: getUsers : function(req, res){ User.find({}, function (err, users) { if (err) return res.status(500).send("There was a problem finding the users."); res.status(200).send(users); }); }, 但是,假设我不想在我的列表中返回birthDate(好吧,我将使用投影),但是我想添加一些额外的属性,我要从其他文档中计算或填充,例如 – numberOfSentMessages,numberOfFollowers等 据我所知,这是一个.net开发人员将使用数据传输对象(DTO)或ViewModel,将有我想返回的属性的情况下,如下所示: UserDTO – name […]

架构和模型是可以的,mongoose。人口没有工作

我检查了我的数据库forms的terminal和数据是好的,但是当填充'commnets'仍然给我的ID 这是我显示数据的路线 var camp_id= req.params.id ; Campground.findById(camp_id).populate("commnets").exec(function(err,fcg){ if(err) { console.log(err) ;} else { console.log("No err") ; console.log(fcg) ; res.render("show",{campground: fcg});} }) ; moongose schemas(营地与评论) var campgroundSchema = new mongoose.Schema( { name: String , image: String , description : String , comments: [ { type: mongoose.Schema.Types.ObjectId, ref: "Commnet" } ] } ) ; var Campground = […]

MongoDB – 错误:文档在保存之前必须有_id

我一直在为这个项目苦苦挣扎。 我正在学习一些在某些领域已经过时的教程,比如他们的Jquery版本在某些function上使用了完全不同的格式,我不得不做很多的改变。 但是我认为我是最后一个难以解决的重大问题。 在我的模式variables中,我有_id,用户名和密码types var UserSchema = new mongoose.Schema({ _id: mongoose.Schema.ObjectId, username: String, password: String }); 但是当我尝试添加一个新的用户到我的应用程序,而不是得到我应该得到的警报,它popup作为[对象对象],没有被添加到数据库。 然后这个错误在mongo cmd中popup “错误:文档在保存前必须有_id”。 我试过注释掉_id这一行,我得到正确的消息,但是在我的数据库中仍然没有显示出来。