ArrayFilter mongo DB

huangapple go评论108阅读模式
英文:

ArrayFilter mongo DB

问题

I'm dealing with a mongo database and my table looks like this.
currently to update everything I use the save method passing it a bean but it doesn't work if I have to update only one element of the array as it overwrites the whole record.

{
"_id" : "21",
"idVlt" : "187",
"giochi" : [
{
"_id" : "22",
"installed" : "true",
"enable" : "true",
"urlimg" : "placeholder.png",
"categories" : [

        ],
        "tags" : [

        ]
    },
    {
        "_id" : "151",
        "installed" : "true",
        "enable" : "false",
        "urlimg" : "placeholder.png",
        "categories" : [

        ],
        "tags" : [

        ]
    },
    {
        "_id" : "197",
        "installed" : "true",
        "enable" : "false",
        "urlimg" : "placeholder.png",
        "categories" : [

        ],
        "tags" : [

        ]
    }
],

}

I would like to understand how I can update the element of the array with _id:22 : "installed":"true" --> false

and being able to insert a new element into the array :

{
"_id" : "222",
"installed" : "true",
"enable" : "true",
"urlimg" : "placeholder.png",
"categories" : [

],
"tags" : [

]

}

with db.AssociazioneGameVlt.find({ $and: [ {"giochi._id":"77"}] })
i've Resutl

英文:

I'm dealing with a mongo database and my table looks like this.
currently to update everything I use the save method passing it a bean but it doesn't work if I have to update only one element of the array as it overwrites the whole record.

{
"_id" : "21",
"idVlt" : "187",
"giochi" : [
    {
        "_id" : "22",
        "installed" : "true",
        "enable" : "true",
        "urlimg" : "placeholder.png",
        "categories" : [

        ],
        "tags" : [

        ]
    },
    {
        "_id" : "151",
        "installed" : "true",
        "enable" : "false",
        "urlimg" : "placeholder.png",
        "categories" : [

        ],
        "tags" : [

        ]
    },
    {
        "_id" : "197",
        "installed" : "true",
        "enable" : "false",
        "urlimg" : "placeholder.png",
        "categories" : [

        ],
        "tags" : [

        ]
    }
],

}

I would like to understand how I can update the element of the array with _id:22 : "installed":"true" --> false

and being able to insert a new element into the array :

 {
    "_id" : "222",
    "installed" : "true",
    "enable" : "true",
    "urlimg" : "placeholder.png",
    "categories" : [

    ],
    "tags" : [

    ]
}

with db.AssociazioneGameVlt.find({ $and: [ {"giochi._id":"77"}] })
i've ArrayFilter mongo DB

答案1

得分: 0

要更新giochi数组中_id为"22"的元素,您可以使用以下更新查询:

db.collection.update({
  "_id": "21",
  "giochi._id": "22"
},
{
  $set: {
    "giochi.$.installed": "false"
  }
})

要添加一个新元素,您可以使用以下代码:

db.collection.update(
   { "_id": "21" },
      { $push: { "giochi": {
          "_id" : "222",
          "installed" : "true",
          "enable" : "true",
          "urlimg" : "placeholder.png",
          "categories": [],
          "tags": []
        } }
      }
    )
英文:

To update the element in the giochi array with the _id: "22" you can use the following update query:

db.collection.update({
  "_id": "21",
  "giochi._id": "22"
},
{
  $set: {
    "giochi.$.installed": "false"
  }
})

And to add a new element you can use below

db.collection.update(
   { "_id": "21" },
      { $push: { "giochi": {
          "_id" : "222",
          "installed" : "true",
          "enable" : "true",
          "urlimg" : "placeholder.png",
          "categories": [],
          "tags": []
        } }
      }
    )

答案2

得分: -1

Here is the translated text:

谢谢,我在我的MongoDB上尝试过,一切正常...
现在我将其翻译成Java代码,如下所示

Query query = new Query(Criteria.where("idVlt").is("148"));
query.addCriteria(Criteria.where("giochi._id").is("197"));
System.out.println("query : " + query);
var update = Update.update("giochi.$.installed", "148_197");
System.out.println("update : " + update);
mongoTemplate.findAndModify(query, update, AssociazioneGameVlt.class);

但是我遇到了这个错误:
org.springframework.data.mongodb.UncategorizedMongoDbException: 服务器上错误2(BadValue):'位置操作符未在查询中找到所需的匹配项。'

然而,如果我用'0'代替'$',它会修改数组的第一个元素而不是获取正确的元素。

英文:

Thanks, i try it on my mongodb and it's ok....
now i traslate it in code java like this

Query query = new Query(Criteria.where("idVlt").is("148"));
	  query.addCriteria(Criteria.where("giochi._id").is("197"));
	  System.out.println("query : "+query);
	  var update = Update.update("giochi.$.installed", "148_197");
	  System.out.println("update : "+update);
	  mongoTemplate.findAndModify(query, update,AssociazioneGameVlt.class );

But i've this error :
org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 2 (BadValue): 'The positional operator did not find the match needed from the query.' on server...

however, if I put '0' instead of '$' it modifies the first element of the array instead of getting the correct one

huangapple
  • 本文由 发表于 2023年6月12日 15:47:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定