简体   繁体   English

如何使用Node.js中的Mongoose使用嵌套数组中object的object id查询和更新嵌套数组对象

[英]how to query and update a nested array objects using Mongoose in Node js using the object id of the object in the nested array

I have a million questions cause I have thought of a million possible ways to do this.我有一百万个问题,因为我想过一百万种可能的方法来做到这一点。 so I know using mongoose and mongodb the object id of a document will never be the same but is it possible that an object id will be reused by when and array in a document assigns object Id's to the objects in nested arrays.所以我知道使用 mongoose 和 mongodb 文档的 object id 永远不会相同但是 object id 是否有可能被重用,文档中的数组将 object Id 分配给嵌套的 82873498 中的对象

I ask because I am trying to query and update using mongoose an array of objects now I noticed that each object in the nested array has its on object Id and I thought I could just find by id and update using that but then I was wonder are those unique to the whole database or just to that document?我问是因为我正在尝试使用 mongoose 查询和更新一个对象数组,现在我注意到嵌套数组中的每个 object 都有它的 object Id,我想我可以通过 id 找到并使用它更新,但后来我想知道是那些对整个数据库或仅对那个文件是唯一的?

then I thought I could write a query that has two parameters the documents obj id and the nested array object that has an obj id then update the name or address.然后我想我可以编写一个具有两个参数的查询,文档 obj id 和嵌套数组 object 具有 obj id,然后更新名称或地址。 the reason I was trying to use both id's is because names can be the same addresses can be the same, even cities can the same in this document or the other documents in the database I figured this 2 obj id's would be unique我尝试同时使用两个 id 的原因是因为名称可以相同地址可以相同,即使城市在本文档或数据库中的其他文档中也可以相同我认为这 2 个 obj id 是唯一的

lastly I wasn't sure if I should be using the $push to update the address of the object because i was wonder if there is an address there already will it delete the address or add to it in some way?最后我不确定我是否应该使用 $push 来更新 object 的地址,因为我想知道那里是否已经有一个地址,它会删除该地址还是以某种方式添加到它? I wasn't sure so I tried $set我不确定所以我尝试了 $set

here is my data这是我的数据

changeAdress: function(req, res) {

db.postData      
.findOneAndUpdate({ _id: req.params.id, people:{$eleMatch:{_id: req.body.person_Id}} },  {$set: {people: req.body.address}},{new:true})
  .then(dbModel => res.json(dbModel))
  .catch(err => res.status(422).json(err));

}, },

"_id" : ObjectId("5f09f40864d7219d546ff9e2"),

"state" : "New Jersey ",

"dateCreated" : ISODate("2020-07-11T17:16:56.898Z"),
"people" : [ 
    {
        
        "_id" : ObjectId("5fb99d00006799751407f843"),
        "address" : "789 main street ",
        "name" : "Roger wilco",
        "city" : "trenton",
        "dateCreated" : ISODate("2020-11-21T23:04:32.753Z")
    }
],
"__v" : 0

} }

I tried this and it worked it doesn't find the document but it lets me update and since the array since I know the the the _id's我尝试了这个并且它没有找到文档但是它让我更新并且因为我知道 _id 的数组

.update({ _id:req.params.id, "comments._id":req.body.commentId},  {$set: {"comments.$.comment": req.body.comment}},{new:true})

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

相关问题 如何使用 mongoose 节点 js 中的聚合使用多个嵌套对象过滤多个嵌套数组 object - How to filter multiple nested array object by using multi nested objects using aggregate in mongoose node js 如何使用Node.js和Mongoose将对象添加到嵌套数组 - How to add object to nested Array using Node.js and Mongoose 如何使用 mongoose 查询嵌套 object 内的数组? - How to query array inside nested object using mongoose? 如何使用 Mongoose Node JS 更新 mongo DB 中另一个嵌套数组中的嵌套数组字段 - How to update a nested array field inside another nested array in mongo DB using Mongoose Node JS 使用 mongoose 在 object 数组中查找嵌套的 object - Finding a nested object in an array of object using mongoose ZCCADCDEDB567ABAE643E15DCF0974E503Z - 如何将 object 推送到嵌套的对象数组中 - Mongoose - How to push object in nested array of objects Node/JS mongoose REST API 如何将新对象推送到一个集合中的嵌套对象数组中 - Node/JS mongoose REST API How to push a new object into a nested array of objects all in one collection 更新数组 Mongoose 中的嵌套 object - update nested object in array Mongoose 猫鼬:更新数组中的嵌套对象 - Mongoose : Update a nested object in an array 如何在Node.js和Mongoose中的对象数组中更新特定对象 - How to update a specific object in a array of objects in Node.js and Mongoose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM