Reputation:
I am trying to create a query object for mongoose, so
let countryCondition = {};
searchQuery = {
"$text": {
"$search": searchString
}
};
query = {
searchQuery,
countryCondition
};
console.log('$query:', query);
When i console.log query, i see the ouput as,
$query: {
searchQuery: {
'$text': {
'$search': '2017'
}
},
countryCondition: {}
}
but i need
[{ '$text': { '$search': '2017' } }, {}]
Upvotes: 0
Views: 151
Reputation: 11
If you'r looking for an or condition your object should be like
{ $or: [ { text: "search text" }, { country: "xyz" } ] }
If you'r looking for an and condition then you can combine two objects following Diego's answer
{text : {}, country : {}}
You can get the first object like
let query = {};
query['$or'] = [];
query['$or'].push({'text' : 'some text'});
query['$or'].push({'country' : 'xyz'});
Upvotes: 0
Reputation: 816
If you really need to combine the objects in one use Object.assign
let countryCondition = {
country: 'Spain'
};
let searchQuery = {
'$text': {
'$search': 'sometext'
}
};
let query = Object.assign(searchQuery, countryCondition);
console.log(query);
Upvotes: 0
Reputation: 385
As Slavik mentioned, you're probably looking for an array rather than an object:
[{ '$text': { '$search': '2017' } }, {}]
since objects must have names for their parameters.
Try this:
let query = [
searchQuery,
countryCondition
];
Upvotes: 2