草庐IT

mongoose-populate

全部标签

node.js - 如何在 Mongoose 中同时更新对象并插入数组

我有一个架构如下varFriendsSchema=newSchema({email:{type:String}firstName:String,lastName:String,previousEmails:[{email:{type:String}}],createdDate:{type:Date,default:Date.now},updatedDate:{type:Date,default:Date.now}});我想做的是,在更新时,如果“电子邮件”发生变化,我想将“电子邮件”字段更新为传入的新值,并将当前的“电子邮件”值推送到'previousEmails'数组。我可以通过以下

node.js - 阻止 Mongoose 为子文档数组项创建 _id 属性

如果您有子文档数组,Mongoose会自动为每个子文档创建ID。示例:{_id:"mainId"subDocArray:[{_id:"unwantedId",field:"value"},{_id:"unwantedId",field:"value"}]}有没有办法告诉Mongoose不要为数组中的对象创建ID? 最佳答案 很简单,你可以在子模式中定义它:varmongoose=require("mongoose");varsubSchema=mongoose.Schema({//yoursubschemacontent},{_id

javascript - Mongoose 保存在 async.each 中创建重复项

我用异步处理了一个数组来保存和验证数据。这是我正在处理的:varguests=[{"email":"first@email.com","name":"Firstguest"},{"email":"second@email.com","name":"Secondguest"}];我正在遍历这个数组并一个一个地保存结果,但是如果我有多个客人,它会在我的mongoose文档中创建重复项。我该如何防止呢?这是处理客人的代码:Posts.findOne({'_id':req.params.id},function(err,post){async.each(guests,function(gues

node.js - 在使用 Mongoose 和 MongoDB 的 API 端点中,如何在向客户端返回响应之前等待所有数据库查询完成

我正在编写一个API端点,用于计算用户过去7天中每一天的总收入。我使用mongoose.find({})方法查询数据库8次。在所有8个.find({})调用返回后,我想向客户端发送响应。我应该将所有.find({})调用与调用下一个.find({})的.then函数链接起来,还是有更简单的方法来等待所有查询完成?为简洁起见,我的路线减去了一些.find({})调用后的样子:app.get('/loadThisWeeksRevenue/:userId',function(req,res,next){//todaysdateinfousingmomentjsvarstartOfToday=

node.js - Mongoose - 将 Schema.Type.Mixed 的嵌套属性查询为指定数据类型(日期)

我在Mongoose中使用设置了一个模式varMySchema=newSchema({data:{type:Schema.Types.Mixed}});我的问题是,在这个“数据”对象上,我将日期值存储为嵌套属性,它一切正常,直到我尝试使用Mongoose执行find()查询并使用嵌套字段进行搜索。因为Mongoose不知道它是一个日期,所以它不能使用我猜测的通常的'$gte'、'$lte'和类似的运算符,因为它将数据视为一个字符串。我的一个对象看起来与此相似{title:"Myobject",data:{publishDate:"2016-07-12T05:00:48.985Z"}我是

arrays - Mongoose 用数组元素的条件填充嵌套数组

我有一个包含文章的文章组,模型看起来像这样varArticleGroup=newmongoose.Schema({articles:[{type:mongoose.Schema.Types.ObjectId,ref:'Article'}]})varArticle=newmongoose.Schema({rates:[{uid:String,rate:Number//user'sratenumbertothisarticle}]})现在一个用户(我们称他为JACK)想要对ArticleGroup执行查询,填充该组中的文章,选择归档的费率通常我会这样做ArticleGroupModel.f

node.js - 丢失的 Mongoose Schema 属性仍在返回中

给定一个看起来像这样的模式:varschema=newmongoose.Schema({name:'string',size:'string'});并且数据库包含集合中所有对象的“名称”。但后来我更改了它并删除了名称varschema=newmongoose.Schema({size:'string'});然后我对其进行查找:schema.find({}).exec().then((objs)=>{//objs[0].namestillexists我认为如果架构没有指定属性,那么它就不会存在于找到的对象上。不是这样吗?删除属性的唯一方法是从mongo中的对象中实际删除它吗?

node.js - 如何正确使用函数 populate()

我使用函数.populate()来获取按类别分组的equipements,所以我的模型是这样的varmongoose=require('../config/db');varEquipementSchema=mongoose.Schema({libelle:String,marque:String,category:{type:mongoose.Schema.Types.ObjectId,ref:'Category'}});module.exports=mongoose.model('Equipement',EquipementSchema);路线:router.get('/catego

node.js - 使用 Mongoose 在对象数组中查找 Schema

这个问题在这里已经有了答案:RetrieveonlythequeriedelementinanobjectarrayinMongoDBcollection(16个答案)关闭5年前。我的Node应用程序中有以下Mongoose模式varexpenseSchema=newSchema({particular:String,date:{type:Date,default:Date.now},paid_by:String,amount:Number,month:String});varroomSchema=newSchema({name:String,admin:String,roomies:

mongodb - 使用 mongoose .update 查询,您可以在更新参数中引用获取的记录吗?

是否有可能用Mongoose或什至只是Mongo做这样的事情?Center.update({ghostBuster:{$exists}},{$set:{ectoplasm:this.exoplasm},$unset:{exoplasm:""}},function(err,result){})我想更新一些记录并将一个字段移到另一个字段中,这样我就可以在更新时引用那条获取的记录。在这种情况下,我使ectoplasm字段具有exoplasm的值是否可以在不在MongooseSchema上定义钩子(Hook)的情况下执行此操作? 最佳答案