Home > Software engineering >  Append one find query result to other mongoose
Append one find query result to other mongoose

Time:10-03

I have two mongoose queries like this:

var doc1=model.find({name:"ABC"});
var doc2=model.find({name:"XYZ"});

I want to append the result of query 2 to query 1. I am doing like this:

doc1.append(doc2);

But above line gives error that doc1.append() is not a function. The obvious question is that if find returns an array of documents, then why append is not available on the result variable.

Can anyone help me to find something which will append the two query results one after the other?

Thank You!

CodePudding user response:

.append() will not work with Array. Use .concat() instead.

The solution should be:

var doc1=model.find({name:"ABC"});
var doc2=model.find({name:"XYZ"});

var appened = doc1.concat(doc2);
  • Related