更新 mongodb 中的深层嵌套数组

Update deeply nested array in mongodb(更新 mongodb 中的深层嵌套数组)

本文介绍了更新 mongodb 中的深层嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新猫鼬中的字段值.

<代码>{_id":ObjectId(5b62c772efedb6bd3f0c983a"),projectID":ObjectId(0000000050e62416d0d75837"),__v":0,clientID":ObjectId(00000000996b902b7c3f5efa"),检查数据":[{pdf":空,已发布":N",提交时间":ISODate(2018-08-02T08:57:08.532Z"),用户ID":对象ID(00000000cac68e3bc04643f7"),"insSummary" : "检查区域","insName" : "信息技术","_id" : ObjectId("5b62c772fa02622a18655e7b"),"published_date" : ISODate("2018-08-02T08:57:22.041Z"),位置方面":[{方面名称":一楼","_id" : ObjectId("5b62c772fa02622a18655e80"),评论" : [{"_id" : ObjectId("5b62c772fa02622a18655e81"),图片" : [{路径":/uploads/inspection/00000000996b902b7c3f5efa/images/1533200242005-IpjLKH4XFWNEcHXa.png","img_name" : "1533200242005-IpjLKH4XFWNEcHXa.png",标题":粉丝",身份证":1"},{路径":/uploads/inspection/00000000996b902b7c3f5efa/images/1533200242008-YN8IlA5yrMn3cBnn.png",img_name":1533200242008-YN8IlA5yrMn3cBnn.png",标题":盒子",身份证":2"}],评论" : [评论4"],推荐" : ""}]}]}

这里我想将图像数组中的标题 Fan 更新为 table fan.

我尝试了 $set 但我不知道如何处理我的数据库结构.

请给个解决办法

**更新:**

我试过这段代码:

mongo.inspection.update({"projectID" : mongoose.Types.ObjectId(req.body.project_id) },{ "$set": {inspection_data.$[e1].locationAspects.$[e2].comments.$[e3].images.$[e4].title":TableFan"}},{数组过滤器":[{ "e1._id": mongoose.Types.ObjectId(req.body.insId)},{ "e2._id": mongoose.Types.ObjectId(req.body.aspectId)},{ "e3._id": mongoose.Types.ObjectId(req.body.commentId)},{e4.id":1"}]},函数(错误,响应){如果(错误){控制台日志(错误")}别的{console.log('更新')控制台日志(响应)}})db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )

它的显示已更新,但在我的数据库中没有任何变化.是不是我做错了什么?

解决方案

你可以试试 arrayFilters in mongodb

var mongoose = require('mongoose')临时更新({ "_id" : mongoose.Types.ObjectId("5b62c772efedb6bd3f0c983a") },{ "$set": {"inspection_data.$[e1].locationAspects.$[e2].comments.$[e3].images.$[e4].title": "TableFan"}},{数组过滤器":[{ "e1._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e7b") },{ "e2._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e80") },{ "e3._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e81") },{e4.id":1"}]})

<块引用>

注意:您必须将 _id 转换为 ObjectId

I am trying to update field value in mongoose.

{
    "_id" : ObjectId("5b62c772efedb6bd3f0c983a"),
    "projectID" : ObjectId("0000000050e62416d0d75837"),
    "__v" : 0,
    "clientID" : ObjectId("00000000996b902b7c3f5efa"),
    "inspection_data" : [ 
        {
            "pdf" : null,
            "published" : "N",
            "submissionTime" : ISODate("2018-08-02T08:57:08.532Z"),
            "userID" : ObjectId("00000000cac68e3bc04643f7"),
            "insSummary" : "inspected areas",
            "insName" : "Infotech",
            "_id" : ObjectId("5b62c772fa02622a18655e7b"),
            "published_date" : ISODate("2018-08-02T08:57:22.041Z"),
            "locationAspects" : [ 
                {
                    "aspectname" : "Ground floor",
                    "_id" : ObjectId("5b62c772fa02622a18655e80"),
                    "comments" : [ 
                        {
                            "_id" : ObjectId("5b62c772fa02622a18655e81"),
                            "images" : [ 
                                {
                                    "path" : "/uploads/inspection/00000000996b902b7c3f5efa/images/1533200242005-IpjLKH4XFWNEcHXa.png",
                                    "img_name" : "1533200242005-IpjLKH4XFWNEcHXa.png",
                                    "title" : "Fan",
                                    "id" : "1"
                                }, 
                                {
                                    "path" : "/uploads/inspection/00000000996b902b7c3f5efa/images/1533200242008-YN8IlA5yrMn3cBnn.png",
                                    "img_name" : "1533200242008-YN8IlA5yrMn3cBnn.png",
                                    "title" : "Box",
                                    "id" : "2"
                                }
                            ],
                            "comment" : [ 
                                "comment4"
                            ],
                            "recommendation" : ""
                        }
                    ]
                }]
}

Here I want to update a title Fan in image array as table fan.

I tried $set but I don't know how to do for my db structure.

Kindly give some solution to this

**Updated:**

I tried this code:

mongo.inspection.update({"projectID" : mongoose.Types.ObjectId(req.body.project_id) },
{ "$set": {

    "inspection_data.$[e1].locationAspects.$[e2].comments.$[e3].images.$[e4].title" : "TableFan"
  }},
  { "arrayFilters": [
    { "e1._id": mongoose.Types.ObjectId(req.body.insId)},
    { "e2._id": mongoose.Types.ObjectId(req.body.aspectId)},
    { "e3._id": mongoose.Types.ObjectId(req.body.commentId)},
    { "e4.id": "1" } 
  ]},function(err,response){
      if(err){
          console.log("error")
      }
      else{
          console.log('Updated')
          console.log(response)
      }
    })

db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )

Its showing updated but in my db there is no change. Is any mistake I did ?

解决方案

You can try with arrayFilters in mongodb

var mongoose = require('mongoose')


Temp.update(
  { "_id" : mongoose.Types.ObjectId("5b62c772efedb6bd3f0c983a") },
  { "$set": {
    "inspection_data.$[e1].locationAspects.$[e2].comments.$[e3].images.$[e4].title": "TableFan"
  }},
  { "arrayFilters": [
    { "e1._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e7b") },
    { "e2._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e80") },
    { "e3._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e81") },
    { "e4.id": "1" }
  ]}
)

Note: You have to cast _id to ObjectId

这篇关于更新 mongodb 中的深层嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:更新 mongodb 中的深层嵌套数组

基础教程推荐