- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
索引帮助页面位于 http://www.mongodb.org/display/DOCS/Indexes没有提到 $elemMatch 并且因为它说要在我的 2M+ 对象集合上添加一个索引,所以我想我会问这个:
我正在做如下查询:
{ lc: "eng", group: "xyz", indices: { $elemMatch: { text: "as", pos: { $gt: 1 } } } }
如果我添加一个索引
{lc:1, group:1, indices.text:1, indices.pos:1}
这个带有 $elemMatch 组件的查询是否能够完全通过索引运行?
最佳答案
根据您的查询,我认为您的文档如下所示:
{
"_id" : 1,
"lc" : "eng",
"group" : "xyz",
"indices" : [
{
"text" : "as",
"pos" : 2
},
{
"text" : "text",
"pos" : 4
}
]
}
我使用这种格式的文档创建了一个测试集合,创建了索引,并运行了您使用 .explain() 选项发布的查询。
索引正在按预期使用:
> db.test.ensureIndex({"lc":1, "group":1, "indices.text":1, "indices.pos":1})
> db.test.find({ lc: "eng", group: "xyz", indices: { $elemMatch: { text: "as", pos: { $gt: 1 } } } }).explain()
{
"cursor" : "BtreeCursor lc_1_group_1_indices.text_1_indices.pos_1",
"isMultiKey" : true,
"n" : NumberLong(1),
"nscannedObjects" : NumberLong(1),
"nscanned" : NumberLong(1),
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : NumberLong(0),
"millis" : 0,
"indexBounds" : {
"lc" : [
[
"eng",
"eng"
]
],
"group" : [
[
"xyz",
"xyz"
]
],
"indices.text" : [
[
"as",
"as"
]
],
"indices.pos" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
},
"server" : "Marcs-MacBook-Pro.local:27017"
}
.explain() 特性的文档可以在这里找到:http://www.mongodb.org/display/DOCS/Explain
.explain() 可用于显示有关查询的信息,包括使用了哪个(如果有)索引。
关于$elemMatch 的 MongoDB 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10207448/
根据 mongodb 文档,$elemMatch 的语法是, t.find( { x : { $elemMatch : { a : 1, b : { $gt : 1 } } } } ) 我试过了,效果
我有以下文件 { "_id" : ObjectId("52da43cd6f0a61e8a5059aaf"), "assignments" : [ {
我正在为我的应用中的项目(产品)进行用户评分(赞/不赞)。 我似乎能够通过对象数组实现我想要的,但我想知道 mongodb 是否提供了一种方法来使用数字数组 (user_ids) 实现这一点。 db.
我有以下文件 { "_id" : ObjectId("52da43cd6f0a61e8a5059aaf"), "assignments" : [ {
我正在为我的应用中的项目(产品)进行用户评分(赞/不赞)。 我似乎能够通过对象数组实现我想要的,但我想知道 mongodb 是否提供了一种方法来使用数字数组 (user_ids) 实现这一点。 db.
这是我的对象: { "_id" : ObjectId("53fdcb6796cb9b9aa86f05b9"), "list" : [ "a", "b" ], "complist" : [ { "a"
我正在尝试查找与条件匹配的数组的最后一个元素,例如,如果我有来自 $elemMatch 的数据页: { _id: 1, students: [ { name: "john", scho
我有以下数据结构 { "_id" : ObjectId("523331359245b5a07b903ccc"), "a" : "a", "b" : [ {
我有一个 unitScores 集合,其中每个文档都有一个 id 和一个文档数组,如下所示: "_id": ObjectId("52134edd5b1c2bb503000001"), "scores"
我的 MongoDB 文档结构如下: {_id: ObjectId("53d760721423030c7e14266f"), fruit: 'apple', vitamins: [ {
考虑以下文档: { "_id" : "ID_01", "code" : ["001", "002", "003"], "Others" : "544554" } 我经历了这个MongoDB
所以,我有一个数据库,其中包含大量文档中的数组。我想使用 $in 找到我的查询与一个或多个数组元素完全匹配的整个文档。 所以,文档结构: { "_id": "76561198045636214",
查询运算符$and的用法有什么逻辑区别吗? db.collection.find({$and: [{"array.field1": "someValue"}, {"array.field2": 3}]
我知道您可以使用 $elemMatch 作为投影来限制子集合数组中的项目。当这样使用它时,它会返回匹配的子文档的所有字段,无论是否也指定了 query projections。 是否可以限制匹配子文档
我有一个像这样的集合(摘要)。 { "id":"summaryid", "locations": [ { "id": "loc1",
我有一个包含以下形式文档的集合: { "_id" : { "$oid" : "67bg............"}, "ID" : "xxxxxxxx", "senses"
我有一个像这样的集合(摘要)。 { "id":"summaryid", "locations": [ { "id": "loc1",
假设我有这样一个文档 { title : 'myTitle', favorites : [{name : 'text', number : 6}, {name : 'other', numbe
我的查询: { 'objects.item.opts1.opts2': { '$elemMatch': [ { name: 'false' } ] } } 返回:数组 opts2 中包含任何内容的任何
如何使用 elemMatch 对 SubDocument 数组进行搜索?我有一个名为 ReportCollection 的文档,其中包含以下元素:- /* 0 */ { "_id" : Obj
我是一名优秀的程序员,十分优秀!