英文:
Issue when trying to query a MongoDB data
问题
我在一个集合中有一个文档,其具有以下属性:
nodeid:长整型
type:字符串
bagid:长整型
因此,节点可以位于一个包中,并且可以是不同类型的。
我需要查找:
所有类型为A的节点,或者在给定节点列表中类型为B的节点,或者在给定包中类型为C的节点。
在MongoDB中,如何设计这个查询?我之前使用了多个IN子句,但从性能的角度来看这不是一个好方法。您能否指导我正确的方向?我尝试过使用这三个元素进行文本搜索,但是文本搜索中的逻辑或操作,例如"type:A type:B node:X" "type:B node:Y"等并不起作用。
谢谢。
编辑,添加示例:
{ "_id" : BinData(3,"NJUuYHEBAAAdCda3V+kXvg=="),
"type" : "question", "bagid" : NumberLong(1067),
"topics" : [ NumberLong(33), NumberLong(67), NumberLong(203), NumberLong(217) ],
"nodeid" : NumberLong(15855),
"creationDate" : ISODate("2020-04-09T18:23:17.812Z"),
"_class" : "com.test.NodeEvent" }
{ "_id" : BinData(3,"NJUuYHEBAAAdCda3V+kXvg=="),
"type" : "comment", "bagid" : NumberLong(1067),
"topics" : [ NumberLong(33), NumberLong(67), NumberLong(203), NumberLong(217) ],
"nodeid" : NumberLong(15857),
"creationDate" : ISODate("2020-04-09T18:23:17.812Z"),
"_class" : "com.test.NodeEvent" }
{ "_id" : BinData(3,"NJUuYHEBAAAdCda3V+kXvg=="),
"type" : "question", "bagid" : NumberLong(1069),
"topics" : [ NumberLong(33), NumberLong(67) ],
"nodeid" : NumberLong(15859), "creationDate" : ISODate("2020-04-09T18:23:17.812Z"),
"_class" : "com.test.NodeEvent" }
英文:
I have a document in a collection that has the following attributes:
nodeid : long
type: string
bagid: long
So, nodes can be on a bag, and be of different types.
I need to find,
all nodes of type A, or nodes of type B in a given list of nodes, or, nodes of type C in a given bag.
How can I design that query in MongoDB? I had all IN clauses but it is the works way to go performance wise. Could you please point me into the right direction? I could not find an aggregation or reduce that would help me make this simpler.
I tried also doing a text search, using the three elements, but the or in the text search, for instance "type: A "type: B node:X" "type: B node: Y" and so on, does not work.
Thanks
Edit, adding samples:
{ "_id" : BinData(3,"NJUuYHEBAAAdCda3V+kXvg=="),
"type" : "question", "bagid" : NumberLong(1067),
"topics" : [ NumberLong(33), NumberLong(67), NumberLong(203), NumberLong(217) ],
"nodeid" : NumberLong(15855),
"creationDate" : ISODate("2020-04-09T18:23:17.812Z"),
"_class" : "com.test.NodeEvent" }
{ "_id" : BinData(3,"NJUuYHEBAAAdCda3V+kXvg=="),
"type" : "comment", "bagid" : NumberLong(1067),
"topics" : [ NumberLong(33), NumberLong(67), NumberLong(203), NumberLong(217) ],
"nodeid" : NumberLong(15857),
"creationDate" : ISODate("2020-04-09T18:23:17.812Z"),
"_class" : "com.test.NodeEvent" }
{ "_id" : BinData(3,"NJUuYHEBAAAdCda3V+kXvg=="),
"type" : "question", "bagid" : NumberLong(1069),
"topics" : [ NumberLong(33), NumberLong(67) ],
"nodeid" : NumberLong(15859), "creationDate" : ISODate("2020-04-09T18:23:17.812Z"),
"_class" : "com.test.NodeEvent" }
答案1
得分: 0
你可以使用 MongoDB 查询语言来构建查询。查询是使用 db.collection.find 方法编写的。它使用各种查询操作符,例如 $or、$in 和 $and。
> 我需要查找所有类型为 A 的节点,或者在给定节点列表中类型为 B 的节点,或者在给定背包中类型为 C 的节点。我该如何在 MongoDB 中设计这个查询?
db.collection.find( { $or: [
{ type: "A" },
{ type: "B", node: { $in: [ "A", "C" ] } },
{ type: "C", bag: 130 }
]
} );
在上述查询中,条件 { type: "C", bag: 130 }
等效于 { $and: [ { type: "C" }, { bag: 130 } ] } }
。对于条件 { type: "B", node: { $in: [ "A", "C" ] } }
也是如此。但在这种情况下使用 $and
是可选的(有关详细信息,请参阅文档)。
> 查询大致是这样的,给定背包 1067,给我所有主题为 33 或 203 的节点
db.collection.find( { bag: 1067, topics: { $in: [ 33, 203 ] } } )
输出取决于集合中的数据。find
方法返回一个 游标,您可以在返回的文档上应用各种游标方法(例如,可以按字段对它们进行排序)。
英文:
You can build your queries using the MongoDB Query Language. The query is written using the db.collection.find method. It uses various query operators like, $or, $in, and $and.
> I need to find, all nodes of type A, or nodes of type B in a given
> list of nodes, or, nodes of type C in a given bag. How can I design
> that query in mongo?
db.collection.find( { $or: [
{ type: "A" },
{ type: "B", node: { $in: [ "A", "C" ] },
{ type: "C", bag: 130 }
]
} );
In the above query the condition { type: "C", bag: 130 }
is equivalent to { $and: [ { type: "C" }, { bag: 130 } ] } }
. This is also the case with the condition { type: "B", node: { $in: [ "A", "C" ] }
. But, using the $and
is optional, in this case (see the documentation for details).
> The query would be something like, given bag 1067 give me all nodes
> with topics: 33 or 203
db.collection.find( { bag: 1067, topics: { $in: [ 33, 203 ] } } )
The output depends upon the data in the collection. The find
method returns a cursor, and you can apply various cursor methods on the retrned documents (for example, you can sort them by a field).
专注分享java语言的经验与见解,让所有开发者获益!
评论