如何知道我的数据库数组中存在哪个数组元素?

时间:2022-10-03 06:59:22

I want to write a query in mongoDB where I want to find which elements of my array are present in database array 'upvotes' my query is given below

我想在mongoDB中编写一个查询,我想在数据库数组中找到我的数组中的哪些元素'upvotes'我的查询在下面给出

userupvotesmodel.findOne({mobile_no: "1234567890", upvotes : { $in : ["aa", "bdnvh", "563fa4408d88ea6c13c7abba", "djfufhgj", "5625bd9dbe545d2412d4ae62", "fjjshddhjfn", "djfhudh", "jfhshnffj", "sjdhfkskajdk"] } }, function(err, docs) {
                                if (err) {
                                    console.log('Error Finding query results');
                                    console.log(err);
                                    res.json({success: 0, message : err});
                                    return next(err);
                                } else {
                                    if (docs) {
                                        console.log('2 mins', currentdate.getMinutes());
                                        console.log('2 secs', currentdate.getSeconds());
                                        console.log('docs', docs);
                                    }
                                }
});

Now for query above if any single value of my array is present in database then it sends whole upvotes array but I am not able to find out which elements were present for e.g "aa", "bdnvh" etc in database how can I write a query to know which elements are present in the database.

现在对于上面的查询,如果我的数组的任何单个值存在于数据库中,那么它发送整个upvotes数组但是我无法找出哪些元素存在例如数据库中的“aa”,“bdnvh”等我怎么能写一个查询以了解数据库中存在哪些元素。

I want to find this in one query by querying for every single element; I know I can do this but I want to do this in any possible single mongoDB query.

我想通过查询每个元素在一个查询中找到它;我知道我可以这样做但我想在任何可能的单个mongoDB查询中执行此操作。

2 个解决方案

#1


1  

I think you don't need to worry about the query and you can just use java script instead. Just iterate through the docs.upvotes array and compare its values with the array you provided.

我认为你不需要担心查询,你可以只使用java脚本。只需遍历docs.upvotes数组并将其值与您提供的数组进行比较。

var my_array = ["aa", "bdnvh", "563fa4408d88ea6c13c7abba", "djfufhgj", "5625bd9dbe545d2412d4ae62", "fjjshddhjfn", "djfhudh", "jfhshnffj", "sjdhfkskajdk"];

// ... your code

// inside of if(docs) statement
var matched_elements = [];
docs.upvotes.forEach(function(element, index, array) {
  for (var i = 0; i < my_array.length; i++) {
    if (my_array[i] == element) {
      matched_elements.push(element);
    }
  }
});
console.log(matched_elements);

#2


1  

You could use the aggregation framework. First $unwind your upvotes array. Then $match on elements that are in the array provided by you. Than $group it back on _id(or whatever you want) creating "matched_elements" array with $addToSet or $push. The returned documents will have only upvotes elements (in "matched_elements array) that are also in your array.

您可以使用聚合框架。首先$展开你的upvotes数组。然后$匹配您提供的数组中的元素。比$ group更回重_id(或任何你想要的)用$ addToSet或$ push创建“matched_elements”数组。返回的文档只有upvotes元素(在“matched_elements数组中”)也在您的数组中。

var my_array = ["aa", "bdnvh", "563fa4408d88ea6c13c7abba", "djfufhgj", "5625bd9dbe545d2412d4ae62", "fjjshddhjfn", "djfhudh", "jfhshnffj", "sjdhfkskajdk"];

db.collection.aggregate( [ 
  { "$unwind" : "$upvotes" }, 
  { "$match": { "upvotes": {"$in": my_array} } },
  { "$group" : { "_id":"$_id","matched_elements":{ "$addToSet": "$upvotes" }}}
] );

#1


1  

I think you don't need to worry about the query and you can just use java script instead. Just iterate through the docs.upvotes array and compare its values with the array you provided.

我认为你不需要担心查询,你可以只使用java脚本。只需遍历docs.upvotes数组并将其值与您提供的数组进行比较。

var my_array = ["aa", "bdnvh", "563fa4408d88ea6c13c7abba", "djfufhgj", "5625bd9dbe545d2412d4ae62", "fjjshddhjfn", "djfhudh", "jfhshnffj", "sjdhfkskajdk"];

// ... your code

// inside of if(docs) statement
var matched_elements = [];
docs.upvotes.forEach(function(element, index, array) {
  for (var i = 0; i < my_array.length; i++) {
    if (my_array[i] == element) {
      matched_elements.push(element);
    }
  }
});
console.log(matched_elements);

#2


1  

You could use the aggregation framework. First $unwind your upvotes array. Then $match on elements that are in the array provided by you. Than $group it back on _id(or whatever you want) creating "matched_elements" array with $addToSet or $push. The returned documents will have only upvotes elements (in "matched_elements array) that are also in your array.

您可以使用聚合框架。首先$展开你的upvotes数组。然后$匹配您提供的数组中的元素。比$ group更回重_id(或任何你想要的)用$ addToSet或$ push创建“matched_elements”数组。返回的文档只有upvotes元素(在“matched_elements数组中”)也在您的数组中。

var my_array = ["aa", "bdnvh", "563fa4408d88ea6c13c7abba", "djfufhgj", "5625bd9dbe545d2412d4ae62", "fjjshddhjfn", "djfhudh", "jfhshnffj", "sjdhfkskajdk"];

db.collection.aggregate( [ 
  { "$unwind" : "$upvotes" }, 
  { "$match": { "upvotes": {"$in": my_array} } },
  { "$group" : { "_id":"$_id","matched_elements":{ "$addToSet": "$upvotes" }}}
] );