假设我的收藏中有以下文件:
{
"_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)
我怎样才能做到这一点?
在MongoDB中,是否可以使用另一个字段中的值更新字段的值?等效的SQL将是这样的:
UPDATE Person SET Name = FirstName + ' ' + LastName
Run Code Online (Sandbox Code Playgroud)
MongoDB伪代码将是:
db.person.update( {}, { $set : { name : firstName + ' ' + lastName } );
Run Code Online (Sandbox Code Playgroud) 我想获取MongoDB集合中所有键的名称.
例如,从这个:
db.things.insert( { type : ['dog', 'cat'] } );
db.things.insert( { egg : ['cat'] } );
db.things.insert( { type : [] } );
db.things.insert( { hello : [] } );
Run Code Online (Sandbox Code Playgroud)
我想得到独特的钥匙:
type, egg, hello
Run Code Online (Sandbox Code Playgroud) 我如何(在MongoDB中)将来自多个集合的数据合并到一个集合中?
我可以使用map-reduce吗?如果是,那么如何?
我非常感谢一些例子,因为我是新手.
我想定义一个$ project聚合阶段,我可以指示它添加一个新字段并包含所有现有字段,而不必列出所有现有字段.
我的文档看起来像这样,有很多字段:
{
obj: {
obj_field1: "hi",
obj_field2: "hi2"
},
field1: "a",
field2: "b",
...
field26: "z"
}
Run Code Online (Sandbox Code Playgroud)
我想做一个像这样的聚合操作:
[
{
$project: {
custom_field: "$obj.obj_field1",
//the next part is that I don't want to do
field1: 1,
field2: 1,
...
field26: 1
}
},
... //group, match, and whatever...
]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我可以使用类似"包含所有字段"的关键字,还是以其他方式避免必须单独列出每个字段?
MongoDB中的Aggregation框架是否有解释功能?我在文档中看不到它.
如果没有其他方法可以检查,查询如何在聚合框架内执行?
我知道发现你就是这么做的
db.collection.find().explain()
Run Code Online (Sandbox Code Playgroud)
但是使用聚合框架我得到一个错误
db.collection.aggregate(
{ $project : { "Tags._id" : 1 }},
{ $unwind : "$Tags" },
{ $match: {$or: [{"Tags._id":"tag1"},{"Tags._id":"tag2"}]}},
{
$group:
{
_id : { id: "$_id"},
"count": { $sum:1 }
}
},
{ $sort: {"count":-1}}
).explain()
Run Code Online (Sandbox Code Playgroud) 是否存在用于计算字段在DB中包含多少个不同值的查询.
我有一个国家的领域,有8种国家价值观(西班牙,英格兰,法国等...)
如果有人在新国家/地区添加了更多文档,我希望查询返回9.
是否有更容易的方式然后分组和计数?
是否可以在$ match中进行OR?
我的意思是这样的:
db.articles.aggregate(
{ $or: [ $match : { author : "dave" }, $match : { author : "john" }] }
);
Run Code Online (Sandbox Code Playgroud) 例如,我有这些文件:
{
"addr": "address1",
"book": "book1"
},
{
"addr": "address2",
"book": "book1"
},
{
"addr": "address1",
"book": "book5"
},
{
"addr": "address3",
"book": "book9"
},
{
"addr": "address2",
"book": "book5"
},
{
"addr": "address2",
"book": "book1"
},
{
"addr": "address1",
"book": "book1"
},
{
"addr": "address15",
"book": "book1"
},
{
"addr": "address9",
"book": "book99"
},
{
"addr": "address90",
"book": "book33"
},
{
"addr": "address4",
"book": "book3"
},
{
"addr": "address5",
"book": "book1"
},
{
"addr": "address77",
"book": "book11"
},
{ …
Run Code Online (Sandbox Code Playgroud) 我如何在mongo集合中找到重复的字段.
我想检查是否有任何"名称"字段是重复的.
{
"name" : "ksqn291",
"__v" : 0,
"_id" : ObjectId("540f346c3e7fc1054ffa7086"),
"channel" : "Sales"
}
Run Code Online (Sandbox Code Playgroud)
非常感谢!