如何在嵌套数组中返回我要查找的元素? [英] How can I return the element I'm looking for inside a nested array?
问题描述
我有一个这样的数据库:
<预><代码>[{宇宙":漫画",传奇":[{名称":x战警",字符":[{字符":金刚狼",图片":618035022351.png"},{字符":独眼巨人",图片":618035022352.png"}]}]},{宇宙":dc",传奇":[{名称":蜘蛛侠",字符":[{字符":毒液",图片":618035022353.png"}]}]}]使用此代码,我设法更新了数组中的一个对象.特别是 character: wolverine
db.mydb.findOneAndUpdate({宇宙":漫画",saga.name":x战警",saga.characters.character":金刚狼"}, {$set:{saga.$[].characters.$[].character":lobezno",saga.$[].characters.$[].picture":618035022354.png",}}, {新:假})
它返回我所有的文档,我只需要匹配的文档
我想返回我更新的对象,而不必对数据库进行更多查询.
注意
有人告诉我,我的代码不能正常工作,显然我的查询更新不好,我想知道如何修复它并获取与这些搜索条件匹配的对象.
换句话说,我怎样才能得到这个输出:
<代码> {字符":金刚狼",图片":618035022351.png"}
在使用过滤器的单个查询中
<代码> {宇宙":漫画",saga.name":x战警",saga.characters.character":金刚狼"}
我的 MongoDB 知识使我无法纠正这一点.
使用 shell 方法 findAndModify
以满足您的需求.
但是您不能使用位置字符 $
在 MongoDb 中投影时不止一次,因此您可能必须在客户端自己跟踪它.
使用 arrayFilters
更新深层嵌套的子文档,而不是位置所有操作符 $[]
.
下面是一个有效的查询 -
var 查询 = {宇宙:'漫画'};变量更新 = {$set:{'saga.$[outer].characters.$[inner].character': 'lobezno','saga.$[outer].characters.$[inner].picture': '618035022354.png',}};var 字段 = {'传奇人物':1};var updateFilter = {数组过滤器:[{'outer.name': 'x战警'},{'inner.character': '金刚狼'}]};db.collection.findAndModify({询问,更新,领域,数组过滤器:updateFilter.arrayFilters新:真实});
I have a database like this:
[
{
"universe":"comics",
"saga":[
{
"name":"x-men",
"characters":[
{
"character":"wolverine",
"picture":"618035022351.png"
},
{
"character":"cyclops",
"picture":"618035022352.png"
}
]
}
]
},
{
"universe":"dc",
"saga":[
{
"name":"spiderman",
"characters":[
{
"character":"venom",
"picture":"618035022353.png"
}
]
}
]
}
]
and with this code I manage to update one of the objects in my array. specifically the object where character: wolverine
db.mydb.findOneAndUpdate({
"universe": "comics",
"saga.name": "x-men",
"saga.characters.character": "wolverine"
}, {
$set: {
"saga.$[].characters.$[].character": "lobezno",
"saga.$[].characters.$[].picture": "618035022354.png",
}
}, {
new: false
}
)
it returns all my document, I need ONLY the document matched
I would like to return the object that I have updated without having to make more queries to the database.
Note
I have been told that my code does not work well as it should, apparently my query to update this bad, I would like to know how to fix it and get the object that matches these search criteria.
In other words how can I get this output:
{
"character":"wolverine",
"picture":"618035022351.png"
}
in a single query using filters
{
"universe": "comics",
"saga.name": "x-men",
"saga.characters.character": "wolverine"
}
My MongoDB knowledge prevents me from correcting this.
Use the shell method findAndModify
to suit your needs.
But you cannot use the positional character $
more than once while projecting in MongoDb, so you may have to keep track of it yourself at client-side.
Use arrayFilters
to update deeply nested sub-document, instead of positional all operator $[]
.
Below is a working query -
var query = {
universe: 'comics'
};
var update = {
$set: {
'saga.$[outer].characters.$[inner].character': 'lobezno',
'saga.$[outer].characters.$[inner].picture': '618035022354.png',
}
};
var fields = {
'saga.characters': 1
};
var updateFilter = {
arrayFilters: [
{
'outer.name': 'x-men'
},
{
'inner.character': 'wolverine'
}
]
};
db.collection.findAndModify({
query,
update,
fields,
arrayFilters: updateFilter.arrayFilters
new: true
});
这篇关于如何在嵌套数组中返回我要查找的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!