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' } }, {}]
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
];
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);
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'});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.