MongoDB select all where field value in a query list

后端 未结 2 597
故里飘歌
故里飘歌 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:31

    Not in a single query it isn't.

    There is nothing wrong with getting the results from a query and feeding that in as your in condition.

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

    But your actual purpose is not clear, as using SQL queries alone as the only example of what you want to achieve are generally not a good guide to answer the question. The main cause of this is that you probably should be modelling differently than as you do in relational. Otherwise, why use MongoDB at all?

    I would suggest reading the documentation section on Data Modelling which shows several examples of how to approach common modelling cases.

    Considering that information, then perhaps you can reconsider what you are modelling, and if you then have specific questions to other problems there, then feel free to ask your questions here.

提交回复
热议问题