Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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
Arrays 如何从用户已选择城市键的数组中获取结果?如果用户选择了城市,则这些将来自城市模型中的未选择城市_Arrays_Node.js_Mongodb_Mongoose_Aggregation Framework - Fatal编程技术网

Arrays 如何从用户已选择城市键的数组中获取结果?如果用户选择了城市,则这些将来自城市模型中的未选择城市

Arrays 如何从用户已选择城市键的数组中获取结果?如果用户选择了城市,则这些将来自城市模型中的未选择城市,arrays,node.js,mongodb,mongoose,aggregation-framework,Arrays,Node.js,Mongodb,Mongoose,Aggregation Framework,城市模型 [ { "_id": '1', "city":'A', }, { "_id": '2', "city":'B', }, { "_id": '3', "city":'C', }, { "_id": '4', "city":'D', }, { "_id": '5', "city":'E', } ] 用户模型 [ { "_id": '1', "us

城市模型

[
  {
    "_id": '1',
    "city":'A',
  },
   {
    "_id": '2',
    "city":'B',
  },
   {
    "_id": '3',
    "city":'C',
  },
  {
    "_id": '4',
    "city":'D',
  },
  {
    "_id": '5',
    "city":'E',
  }
]
用户模型

[
  {
    "_id": '1',
    "user":'sams',
    "selected_city":['1','2']
  } 
]
希望从中获得结果

[

{

  "_id": "1",

  "user": "sams",

"selected_city": [
      "1",
      "2"
    ],

"not_selected": [
      "3",
      "4",
      "5"
    ]
 
  }
]
尝试:

const cities=[
{
“_id”:“1”,
“城市”:“A”,
},
{
“_id”:“2”,
“城市”:“B”,
},
{
“_id”:“3”,
“city”:“C”,
},
{
“_id”:“4”,
“城市”:'D',
},
{
“_id”:“5”,
“城市”:“E”,
}
];
常量用户=[
{
“_id”:“1”,
“user”:“sams”,
“选定城市”:['1','2']
} 
];
const result=users.map(user=>{
user.not_selected=城市
.filter(city=>!user.selected_city.includes(city._id))
.map(城市=>city.\u id)
返回用户;
});

控制台日志(结果)您可以使用下面的聚合

db.coll2.aggregate([
  {
    $lookup: {
      from: "coll1",
      pipeline: [],
      as: "cities"
    }
  },
  {
    $addFields: {
      not_selected: {
        $setDifference: [
          "$cities._id",
          "$selected_city"
        ]
      }
    }
  }
])

这是一种开箱即用的方式

小哈奇可能是

db.userCity.aggregate({$match:{}},
     {$lookup: { from: "cities", localField: "id", foreignField: "id", as: "city"}},      
     //where "id" in localField and "id" in foreignfield are the fields that does not exists in both collections

     {$project:{
          selectedCity: {
               $filter: {
                    input:"$city", 
                    as:"c", 
                    cond: { $in: [ "$$c._id", "$selected_city" ] }
               }
          },
          notSelectedCities: {
               $filter: {
                    input: "$city", 
                    as: "c", 
                    cond:{ $eq: [{ $indexOfArray: [ "$selected_city", "$$c._id" ] }, -1] }
              }
          }
     }}).pretty()
我不知道这是否是最佳实践

但是,正如您在我的$lookup阶段所看到的,我在
localField
foreignField
中使用了“id”,而
用户和
城市的
集合中都不存在“id”

这样,您就可以为用户从
cities
集合中获取所有文档

然后在
$project
阶段,我使用
$filter
过滤结果

然后对于
notSelectedCities
我使用了
$indexOfArray
运算符,如果城市不在所选城市中,它将返回-1,并使用
$eq
运算符将其与-1进行比较,然后仅选择该特定城市


希望这有帮助!:-)

您需要什么输出??输入??嗨,山姆,欢迎来到SO!请添加一些到目前为止您已经尝试过的代码。这些输出{u id:'1','user:'sams','selected_city:['1','2','not_selected:['3','4','5']}我需要这些输出{u id:'1','user:'sams','selected_city:['1','2','not_selected:['3','4','5']}我想要mongodb查询