S
S
Sanchik972021-04-30 14:36:01
Node.js
Sanchik97, 2021-04-30 14:36:01

How to get and check a field from another collection in mongodb?

There are two collections: products, categories. The task is to display products whose category status is true. The product collection looks like this:

Schema({
    name: {
        type: String,
        required: true
    },
  category: {
        type: Schema.Types.ObjectId,
        ref: 'categories'
    },
})


The category collection looks like this:
Schema({
    name: {
        type: String,
        required: true
    },
   status: {
     type: Boolean,
     default: true
   }
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xpaco, 2021-04-30
@xpaco

const workingCategories = await Category
.find({magazineId: req.StoreApiID, status: {$ne: false}}, {"_id": 1})
Products.find({category: {$in: workingCategories}})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question