sql >> Base de Datos >  >> NoSQL >> MongoDB

Buscar usando _id no funciona con agregación

Mongoose no emite String a ObjectId en función agregada. Así que tienes que lanzarlo manualmente usando mangosta.

var mongoose = require('mongoose')

const existedRelation = await this.model.aggregate([
  { "$match": { "_id": mongoose.Types.ObjectId(firstId) } },
  { "$project": {
    "relations": {
      "$filter": {
        "input": "$links",
        "as": "link",
        "cond": {
          "$and": [
            { "$eq": ["$$link.target.entityId", `${objectIdValue}`] },
            { "$eq": ["$$link.linkTypeId", linkTypeId] }
          ]
        }
      }
    }
  }}
])