How to use aggregrate in mongodb to $match _id

前端 未结 3 1320
天涯浪人
天涯浪人 2020-12-15 17:39

Document:

{
    \"_id\" : ObjectId(\"560c24b853b558856ef193a3\"),
    \"name\" : \"Karl Morrison\",
    \"pic\" : \"\",
    \"language\" : ObjectId(\"560c24b         


        
相关标签:
3条回答
  • 2020-12-15 18:20

    Try this

    const User = require('User')
    const mongoose = require("mongoose");
    
    
    User.aggregate([
      {
        $match: { _id: new mongoose.Types.ObjectId('560c24b853b558856ef193a3') }
      }
    ])
    
    0 讨论(0)
  • 2020-12-15 18:24

    use toString() method

    const User = mongoose.model('User')
    User.aggregate([
      {
        $match: { _id: user_id.toString()  }
      }
    ]
    
    0 讨论(0)
  • 2020-12-15 18:41
    const ObjectId = mongoose.Types.ObjectId;
    const User = mongoose.model('User')
    
    User.aggregate([
      {
        $match: { _id: ObjectId('560c24b853b558856ef193a3') }
      }
    ])
    
    0 讨论(0)
提交回复
热议问题