根据猫鼬js中字段的条件查询获取文档

时间:2019-02-12 19:36:36

标签: mongodb mongoose conditional

我有这样的模特

const schema = new mongoose.Schema({
  locationtype: String,        // one of "all","locationstoshow","locationsnottoshow"
  locations: [{
    location_country: String,
    location_state: String,
    location_city: String
  }]
});

export default mongoose.model("AdPost", schema);

现在,我具有当前用户的国家,州和城市值。 我想获得满足这些条件的广告贴文件-

如果locationtype为“ all”,则应返回所有文档,

如果locationtype为“ locationstoshow”,则应检查每个文档是否允许当前用户的国家,州和城市。如果是,应将它们退回。否则不会。

如果locationtype为“ locationsnottoshow”,则应检查每个文档是否在位置中不包括用户的位置并返回。否则不会。

请注意,如果在位置中未提及城市(即location_city:“”)和州/国家,则检查用户的国家/州,如果仅提及国家,则城市和州则为“”。然后,匹配用户的国家/地区。

我在想这样的事情,但它没有给出正确的结果。

let query = [{
        "$cond": {
            "if": {
                "$eq": [ "locationtype", "all" ]
            },
            "then": {
                "$match": [{
                    "$cond": {
                        "if": {
                            "$neq": [ "locations.location_city", "" ]
                        },
                        "then": {
                            "$neq": [ "locations.location_city", "" ]
                        },
                        "else": {
                            "if": {
                                "$neq": [ "locations.location_state", "" ]
                            },
                            "then": {
                                "$eq": [ "locations.location_state", "" ]
                            },
                            "else": {
                                "$eq": [ "locations.location_country", "" ]
                            }
                        }
                    }
                }]
            },
            "else": {
                "if": {
                    "$eq": [ "locationtype", "locationstoshow" ]
                },
                "then": {
                    "$match": [{
                        "$cond": {
                            "if": {
                                "$neq": [ "locations.location_city", "" ]
                            },
                            "then": {
                                "$eq": [ "locations.location_city", `${city}` ]
                            },
                            "else": {
                                "if": {
                                    "$neq": [ "locations.location_state", "" ]
                                },
                                "then": {
                                    "$eq": [ "locations.location_state", `${state}` ]
                                },
                                "else": {
                                    "$eq": [ "locations.location_country", `${country}` ]
                                }
                            }
                        }
                    }]
                },
                "else": {
                    "if": {
                        "$eq": [ "locationtype", "locationsnottoshow" ]
                    },
                    "then": {
                        "$match": [{
                            "$cond": {
                                "if": {
                                    "$neq": [ "locations.location_city", "" ]
                                },
                                "then": {
                                    "$neq": [ "locations.location_city", `${city}` ]
                                },
                                "else": {
                                    "if": {
                                        "$neq": [ "locations.location_state", "" ]
                                    },
                                    "then": {
                                        "$neq": [ "locations.location_state", `${state}` ]
                                    },
                                    "else": {
                                        "$neq": [ "locations.location_country", `${country}` ]
                                    }
                                }
                            }
                        }]
                    },  
                }
            }
        }
    }];

const options = {
    limit: 10,
    page: pageNo
}

AdPost.aggregatePaginate(query, options).then(docs => {
    res.json({ docs });
}).catch(err => {
    res.status(400).json({ errors: err });
})

0 个答案:

没有答案