S
S
Stanislav2017-12-17 09:51:39
MongoDB
Stanislav, 2017-12-17 09:51:39

How, when aggregating records in a project, to find _id in an array and get only it?

Is it possible to find _id when aggregating and leave only it in the array or output true (that it is present in the array)?
Here is the document

likes: [ObjectId('id_user_1'), ObjectId('id_user_2'), ObjectId('id_user_3')]

When fetching, I know the user's _id (sessionId)
.aggregate([
    $project: {
        _id: 1,
        likes: {
             // вывести если найден _id - true, либо false , или оставить только найденный id в массиве или пустой массив
        }
    }
])

Can this be checked?
Tried it and didn't help
likes: { $filter: {
    input: "$likes",
    as: "likes",
    cond: {  "_id": request.sessionId }
}}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question