英文:
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
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论