Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用mongoDB过滤数组对象中的数据?_Node.js_Mongodb - Fatal编程技术网

Node.js 如何使用mongoDB过滤数组对象中的数据?

Node.js 如何使用mongoDB过滤数组对象中的数据?,node.js,mongodb,Node.js,Mongodb,我有一个运行良好的查询,现在我需要过滤数组中的一些数据。我不知道怎么做。下面是我的代码。请告诉我哪里错了 请求数据 [ 'Online Casino/Betting', 'Other ', 'Prefer not to say ', 'Do you really care ? :) ', 'Spend time with friends' ] "interests" : [ { "name" :

我有一个运行良好的查询,现在我需要过滤数组中的一些数据。我不知道怎么做。下面是我的代码。请告诉我哪里错了

请求数据

[
  'Online Casino/Betting',
  'Other ',
  'Prefer not to say ',
  'Do you really care ? :) ',
  'Spend time with friends'
]
"interests" : [
        {
            "name" : "Computers/internet", 
            "_id" : ObjectId("60752406d8e7213e6b5306de"), 
            "id" : NumberInt(1)
        }, 
        {
            "name" : "Astrology/Spiritualism", 
            "_id" : ObjectId("60752406d8e7213e6b5306df"), 
            "id" : NumberInt(3)
        }, 
        {
            "name" : "Cars & motorbikes", 
            "_id" : ObjectId("60752406d8e7213e6b5306e0"), 
            "id" : NumberInt(2)
        }
    ], 
数据库数据

[
  'Online Casino/Betting',
  'Other ',
  'Prefer not to say ',
  'Do you really care ? :) ',
  'Spend time with friends'
]
"interests" : [
        {
            "name" : "Computers/internet", 
            "_id" : ObjectId("60752406d8e7213e6b5306de"), 
            "id" : NumberInt(1)
        }, 
        {
            "name" : "Astrology/Spiritualism", 
            "_id" : ObjectId("60752406d8e7213e6b5306df"), 
            "id" : NumberInt(3)
        }, 
        {
            "name" : "Cars & motorbikes", 
            "_id" : ObjectId("60752406d8e7213e6b5306e0"), 
            "id" : NumberInt(2)
        }
    ], 
查询

if (filterData.interests != undefined && filterData.interests.length > 0) {
  interests = {
    interests: { $elemMatch: { $and: [{ name: filterData.interests }] } }
  }
}

  User.aggregate([
      coordinatesCondition,
      {
        $match: {
          $and: [
            exerciseHabitsCondition,
            interests
          ],
        },
      },
      {
        $sort: lastActivity,
      },
      { $limit: skip + 12 },
      { $skip: skip },
      {
        $lookup: {
          from: "favorites",
          localField: "_id",
          foreignField: "favorites.favoriteUserId",
          as: "favUsers",
        },
      },
    ])

任何解决方案,谢谢

根据我的理解,您希望将结果与req数据中的
interest
匹配。 我正在分享一个简单的更新,它可以很好地为你工作

if(filterData.interests!=未定义&filterData.interests.length>0){
利息查询={
'interests.name':{$in:filterData.interests}
}
}
用户聚合([
协调条件,
{
$match:{
美元及:[
锻炼习惯条件,
利息查询
],
},
},
{
$sort:lastActivity,
},
])

要从聚合中的数组中筛选元素,请使用
$filter
数组运算符。@prasad\uuu请详细说明。实际上,我是以数组格式传递数据的names@robin您能否以最少的聚合阶段提供在调试模式下执行的确切mongoDB查询。