Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB在查询列表中选择所有where字段值_Mongodb_Mongodb .net Driver_Mongo Shell - Fatal编程技术网

MongoDB在查询列表中选择所有where字段值

MongoDB在查询列表中选择所有where字段值,mongodb,mongodb-.net-driver,mongo-shell,Mongodb,Mongodb .net Driver,Mongo Shell,如何在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("53266697c294991f57

如何在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"
}
{
    "_id" : ObjectId("5325ee6efb91c0161cbe7b2c"),
    "hosttype" : "http",
    "useragent" : "Mediapartners-Google",
    "is_crawler" : false,
    "City" : "Mountain View",
    "State" : "CA",
    "Country" : "United States"
}
我有一组
bot
名称

机器人程序集合

{
    "_id" : ObjectId("53266697c294991f57c36e42"),
    "name" : "teoma"
}
{
    "_id" : ObjectId("5325ee6efb91c0161cbe7b2c"),
    "hosttype" : "http",
    "useragent" : "Mediapartners-Google",
    "is_crawler" : false,
    "City" : "Mountain View",
    "State" : "CA",
    "Country" : "United States"
}
我有一个用户流量集合,在该流量集合中,我有一个字段
useragent

用户流量收集

{
    "_id" : ObjectId("53266697c294991f57c36e42"),
    "name" : "teoma"
}
{
    "_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
        })
在这里,我相信它做的等于比较,但我希望它做的像%%comparison

{
    "_id" : ObjectId("53266697c294991f57c36e42"),
    "name" : "teoma"
}
{
    "_id" : ObjectId("5325ee6efb91c0161cbe7b2c"),
    "hosttype" : "http",
    "useragent" : "Mediapartners-Google",
    "is_crawler" : false,
    "City" : "Mountain View",
    "State" : "CA",
    "Country" : "United States"
}
一旦得到结果,我想对该结果集进行更新,即is\u 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
        })
    }

在一个查询中,它不是

从查询中获取结果并将其作为您的条件输入没有什么错

var list=db.collectionA.find({},{u id:0,“field:1}).toArray();
结果=db.collectionB.find({“newfield”:{“$in”:list});
但您的实际目的并不明确,因为仅使用SQL查询作为您想要实现的目标的唯一示例通常不是回答该问题的好指南。造成这种情况的主要原因是,您可能应该以与关系数据库中不同的方式进行建模。否则,为什么要使用MongoDB呢

我建议阅读文档部分,其中展示了几个如何处理常见建模案例的示例


考虑到这些信息,那么也许你可以重新考虑你正在建模的内容,如果你对其他问题有具体的问题,那么请在这里自由提问。

最后,这就是我如何完成的

// 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
                            })
                    } 
                  });
            }
        });

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