MongoDB - Query on the last element of an array?

后端 未结 9 1074
醉梦人生
醉梦人生 2020-11-29 06:51

I know that MongoDB supports the syntax find{array.0.field:\"value\"}, but I specifically want to do this for the last element in the array, which means I don\'

相关标签:
9条回答
  • 2020-11-29 07:33

    use $slice.

    db.collection.find( {}, { array_field: { $slice: -1 } } )
    

    Editing: You can make use of { <field>: { $elemMatch: { <query1>, <query2>, ... } } } to find a match.

    But it won't give exactly what you are looking for. I don't think that is possible in mongoDB yet.

    0 讨论(0)
  • 2020-11-29 07:35

    Not sure about performance, but this works well for me:

    db.getCollection('test').find(
      {
        $where: "this.someArray[this.someArray.length - 1] === 'pattern'"
      }
    )
    
    0 讨论(0)
  • 2020-11-29 07:37

    You can use $expr ( 3.6 mongo version operator ) to use aggregation functions in regular query.

    Compare query operators vs aggregation comparison operators.

    For scalar arrays

    db.col.find({$expr: {$gt: [{$arrayElemAt: ["$array", -1]}, value]}})
    

    For embedded arrays - Use $arrayElemAt expression with dot notation to project last element.

    db.col.find({$expr: {$gt: [{"$arrayElemAt": ["$array.field", -1]}, value]}})
    

    Spring @Query code

    @Query("{$expr:{$gt:[{$arrayElemAt:[\"$array\", -1]}, ?0]}}")
    ReturnType MethodName(ArgType arg);
    
    0 讨论(0)
  • 2020-11-29 07:37

    I posted on the official Mongo Google group here, and got an answer from their staff. It appears that what I'm looking for isn't possible. I'm going to just use a different schema approach.

    0 讨论(0)
  • 2020-11-29 07:37

    You could use $position: 0 whenever you $push, and then always query array.0 to get the most recently added element. Of course then, you wont be able to get the new "last" element.

    0 讨论(0)
  • 2020-11-29 07:43

    In 3.2 this is possible. First project so that myField contains only the last element, and then match on myField.

    db.collection.aggregate([
       { $project: { id: 1, myField: { $slice: [ "$myField", -1 ] } } },
       { $match: { myField: "myValue" } }
    ]);
    
    0 讨论(0)
提交回复
热议问题