0

I'm using https://mongoosejs.com/ for querying mongo

I want to find data of array elements.Like this:

var a = ["a","b","c"];
topic.find({topic:a}).limit(4).exec(.....

If I use like this, I can find only for a element; but I need altogether. That means:

limit for a=4 limit for b=4 limit c=4

At stackoverflow you ask a question and we answer the question. Maybe one answer may be 2 may be 3 and stackoverflow send all comments of answers with limit.And i want to do this.

2
  • I do not fully understand what you're asking here. Could you please clarify that a little bit more using examples of input and output json documents? Commented Oct 1, 2018 at 21:10
  • i want to find data for all array elements;but i use this code mongodb find only for a element;because there is a limit. Commented Oct 1, 2018 at 21:14

2 Answers 2

1

You could use $facet for this which will give you a single result document:

db.collection.aggregate({
    $facet: {
        "a": [{ $match: { "topic": "a" } }, { $limit: 4 }],
        "b": [{ $match: { "topic": "b" } }, { $limit: 4 }],
        "c": [{ $match: { "topic": "c" } }, { $limit: 4 }],
    }
})

If you need separate documents you would probably append the following stages at the end of the above pipeline:

{
    $project: {
        "result": { $concatArrays: [ "$a", "$b", "$c" ] }
    }
}, {
    $unwind: "$result"
}
Sign up to request clarification or add additional context in comments.

5 Comments

error:Unrecognized pipeline stage name: '$facet' :D
Which MongoDB version are you using? $facet exists since v3.4.
may be i change my schemas like this answerschema{comments10:Array,comments20:Array.... } comments10=first 10 comments comments20=11-20 comments
No, it's not deprecated. You just need to be on the right version: mongoosejs.com/docs/api.html#aggregate_Aggregate-facet
sory sory sory sooo sory man:D my mongodb version is 3.0 :D ty for your answer.
0

I think the solution here would be to concatenate the results.

const aIds = topic.find({topic:"a"}).limit(4).map( el => el._id );
const bIds = topic.find({topic:"b"}).limit(4).map( el => el._id );
const cIds = topic.find({topic:"c"}).limit(4).map( el => el._id );
topic.find({_id: {$in: aIds.concat(bIds).concat(cIds) }}).exec(.....

Not the most efficient, but it will work.

UPDATE

Initially, I was not aware that this question related to the mongoose. I'm not familiar with it but the idea remains the same: find 3 topics (a,b,c) each with its own limit then join them.

4 Comments

@sefereken not sure why this happens. What exactly your setup is? Are you using? Is topic a mongo collection instance?
y i work with mongo and mongoose for 1 year;but this problem is hard.
at stackoverflow you ask a question and we answer the question.May be 1 anser may be 2 may be 3 and stackoverflow send all comments of answers with limit(i want to do this:D)
i have topic schema ,answer schema ,and comment schema.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.