MongoDB select all where field value in a query list

后端 未结 2 601
故里飘歌
故里飘歌 2021-01-14 20:51

How to achieve below SQL in MongoShell?

Select TableA.* from TableA where TableA.FieldB in (select TableB.FieldValue from TableB)

Mongo doc

2条回答
  •  滥情空心
    2021-01-14 21:38

    Finally this is how I could accomplish it.

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

提交回复
热议问题