Javascript $inc+;addToSet和populate-NodeJS-Mongoose
我想在ref对象中包含变量: 用户模型Javascript $inc+;addToSet和populate-NodeJS-Mongoose,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我想在ref对象中包含变量: 用户模型 var userSchema = new mongoose.Schema({ pseudo : { type : String }, fullname : { type : String }, password : { type : String }, email : { type : String }, //tags : { type : Array }, tags : [ {
var userSchema = new mongoose.Schema({
pseudo : { type : String },
fullname : { type : String },
password : { type : String },
email : { type : String },
//tags : { type : Array },
tags : [
{
objectId : { type : mongoose.Schema.Types.ObjectId, ref: 'tag'},
completed : { type : Boolean, default : false }
}
],
dateCreation : { type : Date, default : Date.now }
});
var tagSchema = new mongoose.Schema({
name : { type : String },
alias : { type : String },
description : { type : String },
countMembers : { type : Number, default : 0 },
dateCreation : { type : Date, default : Date.now }
});
tagModel
var userSchema = new mongoose.Schema({
pseudo : { type : String },
fullname : { type : String },
password : { type : String },
email : { type : String },
//tags : { type : Array },
tags : [
{
objectId : { type : mongoose.Schema.Types.ObjectId, ref: 'tag'},
completed : { type : Boolean, default : false }
}
],
dateCreation : { type : Date, default : Date.now }
});
var tagSchema = new mongoose.Schema({
name : { type : String },
alias : { type : String },
description : { type : String },
countMembers : { type : Number, default : 0 },
dateCreation : { type : Date, default : Date.now }
});
我试过:
userModel.findByIdAndUpdate(
req.session.passport.user._id,
{ $inc: { "tags.objectId.countMembers": 1 }, $addToSet: {"tags": { objectId: mongoose.Types.ObjectId(publicTag._id.toString()) }}},
{ upsert: true },
function(err, model) {
if (err)
throw err;
});
和
userModel.findByIdAndUpdate(
req.session.passport.user._id,
{ $addToSet: {"tags": { objectId: mongoose.Types.ObjectId(publicTag._id.toString()) }}},
{ $inc: { "tags.objectId.countMembers": 1 }, upsert: true },
function(err, model) {
if (err)
throw err;
});
我收到一个错误“无法使用零件…”:(
也许我必须使用填充?我不知道
如果一个人有一个好的网站/链接来理解查询猫鼬中的额外选项
每次我在3个小时内查找问题,只是为了使用一个查询,我混合了几个选项,例如$inc和upsert
谢谢,Populate不是“加入”魔法。MongoDB一次作用于单个集合。因此,除非数据实际嵌入,否则不单独作用于源集合(而不是父集合)就无法执行此操作。