A
A
Anton2021-04-16 16:29:56
MongoDB
Anton, 2021-04-16 16:29:56

How to query in MongoDB query results?

Hello.
I read a lot of things, but there was no understanding, and no. And the request is very popular.
Step1: Get a list of guids of all organizations with the specified conditions

db.organizations.find({
    docStatus: {
        $ne: "delete"
    },
    isAuthorized: true
}, {
    _id: 0,
    guid: 1
})


Step2: Looking for users whose org field matches the guids of the previous request, and the status is active:true
db.users.find({
org:"guid_из_шаг1", active:true
})


How to solve this problem with one simple request?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2021-04-19
@hunk3r

db.users.aggregate({$match: {active:true}},
{$lookup:
{ from: "org", localField: "org.id", foreignField: "guid", as: "org"},
{$unwind: "$org"},
{$match: {"org.docStatus": {$ne: "delete"}, "org.isAuthorized": true},
{$group: { _id: "$_id"}})

D
Dmitry Belyaev, 2021-04-17
@bingo347

https://docs.mongodb.com/manual/reference/operator...
https://docs.mongodb.com/manual/reference/operator...
https://docs.mongodb.com/manual/reference/ operator...
In general, Monga is not intended for this... You need to choose a database for the task, and not follow the fashion...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question