【发布时间】:2014-03-17 03:36:16
【问题描述】:
如何在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
})
这里我相信它在做 equals to comparison,但我希望它做 like %% comparison
获得结果后,我想将该结果集更新为 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
})
}
【问题讨论】:
标签: mongodb mongodb-.net-driver mongo-shell