简体   繁体   English

Mongodb更新推送对象数组

[英]Mongodb update push Array of Objects

I am unable to solve this issue (and looking to avoid loop update one by one), please hep me out here. 我无法解决这个问题(并希望逐个避免循环更新),请在这里解答我。

I have a fambio document (which has its own FamilyModel) which gets created after user gives his below information: 我有一个fambio文档(有自己的FamilyModel),在用户提供以下信息后创建:

{
  name: 'john',
  lname: 'doe',
}

Now, after above information gets saved, user provides more information about the family after some processing in backend: 现在,在保存上述信息后,用户在后端进行一些处理后会提供有关该系列的更多信息:

  let familyArr = [
    { _id: 1234, name: 'Jenny', lname: 'doe', relation: 'mother' },
    { _id: 2345, name: 'Jawn', lname: 'doe', relation: 'father' },
    { _id: 3456, name: 'Jane', lname: 'doe', relation: 'sister' },
    { _id: 4567, name: 'Daisy', lname: 'wick', relation: 'pupper' }
  ]

Altogether, FamilyModel schema looks like: 总而言之,FamilyModel架构如下所示:

const FamilyModel = mongoose.Schema({
  name: {type: String, required: true},
  lname: {type: String, required: true},
  family: [relationshipSchema]
}, {
  timestamp: true
});

const relationshipSchema = mongoose.Schema({
  name: {type: String, required: true},
  lname: {type: String, required: true},
  relation: {type: String, required: true}
}, {
  required: false,
  timestamp: true
});

Now, John has an array of objects of family (filed type Array) and trying to insert that Array of Object like this: 现在,John有一系列对象的数组(字段类型为Array),并试图像这样插入对象数组:

What I tried multiple options: 我尝试了多种选择:

    db.fambio.updateOne({_id: 1111}, { $set: { family: familyArr }})
    db.fambio.findOneAndUpdate({_id: 1111}, { $push: { family: familyArr }});
    db.fambio.update({_id: 1111}, $addToSet: { 'family': familyArr}});

Nothing is working with respect to insert the constructed Object directly to the field. 关于将构造的Object直接插入到字段中,没有任何工作。 When I insert one at a time, it gets updated. 当我一次插入一个时,它会更新。

How am I supposed to write the query to update/append an Array of Objects in to a field of Array Type having its own Schema maintained. 如何编写查询以将对象数组更新/追加到具有自己的Schema维护的Array Type字段中。

Okay, I've solved it. 好的,我已经解决了。 How I solved it: 我是如何解决的:

Initially, I saved the document and did some processing on the info as per my requirements post which I wanted to insert Array of Elements in to an Array key field where I was running in to the issue of nothing getting inserted. 最初,我保存了文档,并按照我的要求帖子对信息进行了一些处理,我希望将数组元素插入到我正在运行的数组键字段中,以便插入任何内容。 What I was missing was $each and this is how I did it: 我缺少的是$ each,这就是我做的:

db.getCollection('fambio').update({
      _id: johnsuserid
    }, {
      $push: {
        'family': {
          $each:[
                  { _id: 1234, name: 'Jenny', lname: 'doe', relation: 'mother' },
                  { _id: 2345, name: 'Jawn', lname: 'doe', relation: 'father' },
                  { _id: 3456, name: 'Jane', lname: 'doe', relation: 'sister' },
                  { _id: 4567, name: 'Daisy', lname: 'wick', relation: 'pupper' }
                ]
        }
      }
    });

Thank you everyone!! 谢谢大家!! Hope this helps someone who may face this problem in future. 希望这可以帮助将来可能面临这个问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM