[英]How to find matches in mongodb for all elements in a javascript array?
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. 我正在尝试搜索为数组中每个不同元素创建的mongodb数据库中的所有匹配项。 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
如果有人可以向我指出为什么它不能正确查询mongodb数据库,那将是可取的
$in
expects an array, so you'd have to pass the fiatList as parameter. $in
需要一个数组,因此您必须将fiatList作为参数传递。 That is, if you are looking for a document where quote: ['EUR', 'AUD', 'BRL', 'CAD', 'CHF', 'CNY', 'CZK', 'DKK']
也就是说,如果您正在寻找
quote: ['EUR', 'AUD', 'BRL', 'CAD', 'CHF', 'CNY', 'CZK', 'DKK']
包含以下内容的文档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: 例如:
quote:['EUR', 'AUD'], quote:['CZK', 'CHF']
然后,您可以这样做:
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 这样,如果
quote:['CZK', 'CHF']
可以得到相同的文档,因为
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.
我之前听说过有人用大写的猫鼬创建该系列,但在mongodb中将其写为小写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.