2012-12-13 70 views
0

我想在mongodb中拆分我的长查询,例如在下面的代码中使用db.users.find(q),我想使用像这样的连接版本db.users.find(q0 + q1)或任何其他方式,我可以将q分成两个查询在mongodb中连接两个查询

q= {},{name:1,"education.school.name":1,"education.type":1} 
db.users.find(q) 
q0={} 
q1={name:1,"education.school.name":1,"education.type":1} 
q=q0+","+q1 
db.users.find(q) 

我该怎么做?

+0

我们在谈论什么编程语言? –

+0

mongodb,不使用任何java代码 – user1848018

回答

0

您的条款是有些模棱两可的有(你qq0都是空的对象),但你可能想在$or操作

db.users.find(
    $or: [ 
    {name:1,"education.school.name":1,"education.type":1}, 
    {age:1,"education.school.name":2,"education.type":2} 
    ] 
) 

这将返回这两个查询的并集。也就是说,根据你实际想要做什么,如果你只是想搜索多个值,你可能可以使用$in和一系列值来搜索每个字段。

+0

但是如何使q0 = {name:1,“education.school.name”:1,“education.type”:1}和q1 = {age:1,“education.school。名称“:2,”education.type“:2},如何在db.users.find中使用q0和q1? – user1848018

+0

如果你有两个子句,'$或'是合适的。 –

+0

我试过db.users.find($或者:[q0,q1])其中q0和q1上面的查询,我尝试了几个组合,但都没有工作 – user1848018