Find document with array that contains a specific value

前端 未结 10 1319
猫巷女王i
猫巷女王i 2020-11-22 04:06

If I have this schema...

person = {
    name : String,
    favoriteFoods : Array
}

... where the favoriteFoods array is popula

相关标签:
10条回答
  • 2020-11-22 04:15

    Though agree with find() is most effective in your usecase. Still there is $match of aggregation framework, to ease the query of a big number of entries and generate a low number of results that hold value to you especially for grouping and creating new files.

      PersonModel.aggregate([
                { 
                     "$match": { 
                         $and : [{ 'favouriteFoods' : { $exists: true, $in: [ 'sushi']}}, ........ ]  }
                 },
                 { $project : {"_id": 0, "name" : 1} }
                ]);
    
    0 讨论(0)
  • 2020-11-22 04:17

    In case you need to find documents which contain NULL elements inside an array of sub-documents, I've found this query which works pretty well:

    db.collection.find({"keyWithArray":{$elemMatch:{"$in":[null], "$exists":true}}})
    

    This query is taken from this post: MongoDb query array with null values

    It was a great find and it works much better than my own initial and wrong version (which turned out to work fine only for arrays with one element):

    .find({
        'MyArrayOfSubDocuments': { $not: { $size: 0 } },
        'MyArrayOfSubDocuments._id': { $exists: false }
    })
    
    0 讨论(0)
  • 2020-11-22 04:25

    For Loopback3 all the examples given did not work for me, or as fast as using REST API anyway. But it helped me to figure out the exact answer I needed.

    {"where":{"arrayAttribute":{ "all" :[String]}}}

    0 讨论(0)
  • 2020-11-22 04:26

    In case that the array contains objects for example if favouriteFoods is an array of objects of the following:

    {
      name: 'Sushi',
      type: 'Japanese'
    }
    

    you can use the following query:

    PersonModel.find({"favouriteFoods.name": "Sushi"});
    
    0 讨论(0)
  • 2020-11-22 04:26

    If you'd want to use something like a "contains" operator through javascript, you can always use a Regular expression for that...

    eg. Say you want to retrieve a customer having "Bartolomew" as name

    async function getBartolomew() {
        const custStartWith_Bart = await Customers.find({name: /^Bart/ }); // Starts with Bart
        const custEndWith_lomew = await Customers.find({name: /lomew$/ }); // Ends with lomew
        const custContains_rtol = await Customers.find({name: /.*rtol.*/ }); // Contains rtol
    
        console.log(custStartWith_Bart);
        console.log(custEndWith_lomew);
        console.log(custContains_rtol);
    }
    
    0 讨论(0)
  • 2020-11-22 04:30

    I know this topic is old, but for future people who could wonder the same question, another incredibly inefficient solution could be to do:

    PersonModel.find({$where : 'this.favouriteFoods.indexOf("sushi") != -1'});
    

    This avoids all optimisations by MongoDB so do not use in production code.

    0 讨论(0)
提交回复
热议问题