Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $inc+;addToSet和populate-NodeJS-Mongoose_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript $inc+;addToSet和populate-NodeJS-Mongoose

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 : [ {

我想在ref对象中包含变量:

用户模型

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个小时内查找问题,只是为了使用一个查询,我混合了几个选项,例如$incupsert

谢谢,

Populate不是“加入”魔法。MongoDB一次作用于单个集合。因此,除非数据实际嵌入,否则不单独作用于源集合(而不是父集合)就无法执行此操作。