假设我的收藏中有以下文件:
{
"_id":ObjectId("562e7c594c12942f08fe4192"),
"shapes":[
{
"shape":"square",
"color":"blue"
},
{
"shape":"circle",
"color":"red"
}
]
},
{
"_id":ObjectId("562e7c594c12942f08fe4193"),
"shapes":[
{
"shape":"square",
"color":"black"
},
{
"shape":"circle",
"color":"green"
}
]
}
Run Code Online (Sandbox Code Playgroud)
查询:
db.test.find({"shapes.color": "red"}, {"shapes.color": 1})
Run Code Online (Sandbox Code Playgroud)
要么
db.test.find({shapes: {"$elemMatch": {color: "red"}}}, {"shapes.color": 1})
Run Code Online (Sandbox Code Playgroud)
返回匹配的文档(文档1),但始终包含所有数组项shapes:
{ "shapes":
[
{"shape": "square", "color": "blue"},
{"shape": "circle", "color": "red"}
]
}
Run Code Online (Sandbox Code Playgroud)
但是,我想仅使用包含以下内容的数组来获取文档(文档1)color=red:
{ "shapes":
[
{"shape": "circle", "color": "red"}
]
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
所以我通过mongoose的id查询了一个特定的文档.该文档包含一系列项目.此列表将始终非常小(少于10个项目).我想知道是否有办法从阵列中获取特定项目.
Example doc:
{
_id: 1,
name: 'some name,
items: [{
id: 23,
name: 'item name 23'
},{
id: 24,
name: 'item name 24'
}]
}
Run Code Online (Sandbox Code Playgroud)
从mongoose docs我可以得到items数组:
doc.get('items')
Run Code Online (Sandbox Code Playgroud)
从那里我可以迭代数组并找到我想要的东西,这没什么大不了的.如果框架中提供了这个,就不要重新发明轮子.
我是 MongoDB 的新手。我以以下格式将数据存储在 mongoDB 中
"_id" : ObjectId("51d5725c7be2c20819ac8a22"),
"chrom" : "chr22",
"pos" : 17060409,
"information" : [
{
"name" : "Category",
"value" : "3"
},
{
"name" : "INDEL",
"value" : "INDEL"
},
{
"name" : "DP",
"value" : "31"
},
{
"name" : "FORMAT",
"value" : "GT:PL:GQ"
},
{
"name" : "PV4",
"value" : "1,0.21,0.00096,1"
}
],
"sampleID" : "Job1373964150558382243283"
Run Code Online (Sandbox Code Playgroud)
我想将值更新为 11,其名称为Category. 我试过下面的查询:
db.VariantEntries.update({$and:[ { "pos" : 117199533} , { "sampleID" : "Job1373964150558382243283"},{"information.name":"Category"}]},{$set:{'information.value':'11'}})
Run Code Online (Sandbox Code Playgroud)
但是 Mongo 回复了
can't …Run Code Online (Sandbox Code Playgroud)