草庐IT

mongoose-dbref

全部标签

node.js - 如何使用 Mongoose 更新 mongodb 中的对象?

我有一个架构:exports.participant=function(mongodb){varparticipantSchema=newmongoose.Schema({uniqueid:{type:String,unique:true},channel:String,conference:String,calleridnum:String,calleridname:String,event:String,status:{type:Boolean,default:0}},{collection:'participants'});varparticipant=mongodb.model

node.js - Mongoose:如何在填充期间排除虚拟 id 字段

我试图排除Mongoose设置的虚拟id字段。varBar=newSchema({body:String});varFoo=newSchema({bars:type:Schema.Types.ObjectId,ref:'Bar'});Foo.find({..query...}).populate('bars','body-_id').exec(function(err,foos){console.log(foos);//{bars:[{id:null,body:'bodystring'}]}});如何删除“id”字段?因为我在填充中排除了_id,它还是null吗?

node.js - MongoDB:如何使用 Mongoose 添加或更新子文档?

我正在尝试添加一个新的或更新一个现有的SubDocument,这是基于它的存在。给出的是以下架构:varproviderSchema=newSchema({provider:String,id:String,accessToken:Object})varuserSchema=newSchema({email:{type:String},provider:[providerSchema],created_at:Date,updated_at:Date});存储以下文档:{"_id":{"$oid":"5629f57b13bc066c17b88a0d"},"created_at":{"$d

node.js - 反向填充 Mongoose

我正在尝试了解如何使用我的订单集合中的businessId属性来填充我的业务订单。我试过了,但无法正常工作。https://www.npmjs.com/package/mongoose-reverse-populate知道我做错了什么或关于如何完成我需要做的事情的其他建议吗?Business.findById(id).exec(function(err,business){varopts={modelArray:business,storeWhere:"orders",arrayPop:true,mongooseModel:Order,idField:"businessId"};rev

node.js - Mongoose 从数组元素中选择特定字段

假设我有这个模式{jedi:[{name:Stringlightsaber_color:String]}}我想返回全部并且只返回他们的名字。我试过了Jedi.find({}).select('jedi.name').exec(function(err,jedi){if(err){console.log("nothingfound")}}它什么都不返回,而这段代码返回所有内容。Jedi.find({}).select('jedi').exec(function(err,jedi){if(err){console.log("nothingfound")}}我看到jedi是一个数组,所以我认

node.js - MongoDB 使用 Mongoose 保存问题

我在使用Mongoose在我的mongodb中保存数据时遇到了一些问题。这是我使用的代码:////////////////////////////////////////////Mongoosetestvarurl=mongoUrl+dbCollectionName;varmongoose=require('mongoose');mongoose.connect(url);vardb=mongoose.connection;db.on('error',console.error.bind(console,'connectionerror:'));db.once('open',funct

node.js - Mongoose 通过id方法删除子文档

我有两个模型:varmongoose=require('mongoose');varSchema=mongoose.Schema;varProjectSchema=newSchema({title:{type:String},images:[{type:Schema.Types.ObjectId,ref:'Image'}]});module.exports=mongoose.model('Project',ProjectSchema);和varmongoose=require('mongoose');varSchema=mongoose.Schema;varImageSchema=ne

node.js - 如何从父实例访问 Mongoose 中的子文档实例?

varmongoose=require('mongoose');varSchema=mongoose.Schema;varChildSchema=newSchema({name:String});ChildSchema.methods.getName=function(){returnthis.name;}varParentSchema=newSchema({children:{type:[ChildSchema]default:[]}});ParentSchema.methods.getChildName=function(){//Howtofacilitateabilitytoac

javascript - Mongoose:如何捕获由异步回调引起的 MongoError

当我试图将太大的数据推送到mongoDb时,我收到此错误:MongoError:documentislargerthanthemaximumsize16777216我知道,这是正常的,但我无法捕捉到这个错误,我的node.js进程退出了。你能告诉我,如何捕获这个错误吗?example.js:无法捕获错误,node进程退出varmongoose=require('mongoose');mongoose.connect('mongodb://localhost/test');mongoose.connection.on('error',function(err){console.log(e

node.js - 为什么不能修改 Mongoose 查询返回的数据(例如 : findById)

当我尝试更改Mongoose查询返回的数据的任何部分时,它没有任何效果。我昨天花了大约2个小时试图解决这个问题,使用各种_.clone(),使用临时存储变量等。最后,就在我想我要去的时候疯了,我找到了解决办法。所以我想将来有人(fyuuuuture!)可能会遇到保存问题。Survey.findById(req.params.id,function(err,data){varlen=data.survey_questions.length;varcounter=0;_.each(data.survey_questions,function(sq){Question.findById(sq