I am trying to attempt to search all matches in the mongodb database that I have created for each of the different elements in an array. I have the following code
var fiatList = ['EUR', 'AUD', 'BRL', 'CAD', 'CHF', 'CNY', 'CZK', 'DKK'];
function updateFiatTotal() {
fiatList.forEach(function(entry){
console.log('Calculating Fiat total data');
models.Market.find({ quote: { $in: entry } }, function(err, markets) {
if (err) return console.log(err);
console.log('markets');
});
});
}
For some reason this is not returning anything for the markets that I am trying to print out into the database. If anyone could give me pointers on why it is not correctly querying the mongodb database it would be apprecaited
$in
expects an array, so you'd have to pass the fiatList as parameter. That is, if you are looking for a document where quote: ['EUR', 'AUD', 'BRL', 'CAD', 'CHF', 'CNY', 'CZK', 'DKK']
models.Market.find({
quote: fiatList
}, function(err, markets) {
if (err) return console.log(err);
console.log('markets');
});
Now if you are looking for a document that could have one of these values. Example: quote:['EUR', 'AUD'], quote:['CZK', 'CHF']
Then you could do this:
fiatList.forEach(function(entry) {
models.Market.find({
quote: entry
}, function(err, markets) {
if (err) return console.log(err);
console.log('markets');
});
});
This way you could get the same document if quote:['CZK', 'CHF']
, cause you would get it once for
models.Market.find({
quote: 'CZK'
})
and once for
models.Market.find({
quote: 'CHF'
})
Also if you still don't get any results, check if your collection is called market instead of Market . I've heard before that people created the collection in mongoose with capital but in mongodb it was written lowercase.
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.