MongoDB查询所有符合条件的记录,其中字段值在查询列表中。

3
如何在MongoShell中实现以下SQL?
Select TableA.* from TableA where TableA.FieldB in (select TableB.FieldValue from TableB)

Mongo doc 提供了一些示例。
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )

我希望数组可以根据另一个查询动态生成。这可行吗?

扩展我的问题

我有一个机器人名称的集合。

机器人集合

{
    "_id" : ObjectId("53266697c294991f57c36e42"),
    "name" : "teoma"
}

我有一个用户流量的集合,在这个流量集合中,我有一个字段 useragent

用户流量集合

{
    "_id" : ObjectId("5325ee6efb91c0161cbe7b2c"),
    "hosttype" : "http",
    "useragent" : "Mediapartners-Google",
    "is_crawler" : false,
    "City" : "Mountain View",
    "State" : "CA",
    "Country" : "United States"
}

我希望选择所有用户流量文件,其中的useragent包含bot集合中的任何名称。
这是我想到的。
var botArray = db.bots.find({},{name:1, _id:0}).toArray()

db.Sessions.find({
    useragent: {$in: botArray}
    },{ 
        ipaddress:1
        })

我认为这里正在进行等于比较,但我希望它进行类似于%%的比较

一旦我得到结果,我想对该结果集进行更新,设置为is_crawler= true

尝试了类似这样的东西,没有什么帮助

db.bots.find().forEach( function(myBot) {
    db.Sessions.find({
        useragent: /myBot.name/
        },{ 
            ipaddress:1
            }) 
     });

另一种循环浏览记录的方法,但没有找到匹配项。

var bots = db.bots.find( {
    $query: {}, 
    $orderby:{
        name:1}
        });
while( bots.hasNext()) {
    var bot = bots.next();
    //print(bot.name);
    var botName = bot.name.toLowerCase();
    print(botName);
     db.Sessions.find({
        useragent: /botName/,
        is_crawler:false
        },{ 
            start_date:1,
            ipaddress:1,
            useragent:1,
            City:1,
            State:1,
            Country:1,
            is_crawler:1,
            _id:0
        })
    }
2个回答

7

这并不能在一次查询中完成。

从查询结果中获取并将其作为条件输入并没有什么问题。

var list = db.collectionA.find({},{ "_id": 0, "field": 1 }).toArray();

results = db.collectionB.find({ "newfield": { "$in": list } });

但是你的实际目的并不清楚,仅使用SQL查询作为想要实现的唯一示例通常不是回答问题的好指南。这主要是因为你可能应该进行不同于关系模型的建模。否则,为什么要使用MongoDB呢?
我建议阅读数据建模部分的文档,其中展示了如何处理常见建模案例的多个示例。
考虑到这些信息,也许你可以重新考虑你正在建模的内容,如果有其他问题需要问,那么请随时在这里提出你的问题。

如何使用类似语句“newfield”:{“$like”:list}来实现相同的效果。 - HaBo
扩展了我的问题,看看在Mongo建模中是否有意义 - HaBo

0

最终我是这样完成的。

// Get a array with values for name field
var botArray = db.bots.find({},{name:1}).toArray();

// loop through another collection
        db.Sessions.find().forEach(function(sess){
            if(sess.is_crawler == false){ // check a condition
                 // loop in the above  array
                botArray.forEach(function(b){
                //check if exists in the array
                   if(String(sess.useragent).toUpperCase().indexOf(b.name.toUpperCase()) > -1){
                        db.Sessions.update({ _id : sess._id} // find by _id
                        ,{
                            is_crawler : true // set a update value
                            },
                            {
                                upsert:false // do update only
                            })
                    } 
                  });
            }
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接