如何在MongoShell中实现SQL以下?
Select TableA.* from TableA where TableA.FieldB in (select TableB.FieldValue from TableB)
Mongo doc提供了一些示例
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
我希望该数组可以从另一个查询中动态获取。是否有可能?
扩展我的问题
我有一个bot
名字集合
机器人集合
{
"_id" : ObjectId("53266697c294991f57c36e42"),
"name" : "teoma"
}
我有一个用户流量集合,在该流量集合中,我有一个字段 useragent
userTraffic集合
{
"_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
})
}
最后,这就是我可以实现的方式。
// 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
})
}
});
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句