我正在开发mongodb和nodejs中的一个城市过滤器,其中的城市列表和复选框如下
一旦我们点击任何城市或多个城市,然后我们收到与该城市相关的数据,代码如下。
var query = {city:{ $in:city }};
posts.find(query, {} , function(e,docs){
res.json(docs);
});通过查询,我将城市作为数组传递并接收Data..But,我的要求是:
1.假设如果我选择了1或2座城市,那么该城市的结果将是第一位的,即使那些城市没有被选中,其余的城市也会跟随在该城市之后。
谢谢。
发布于 2015-09-25 08:39:35
Query1 : db.city.find({name:{$in:["Bangalore"]}})
Query2 - db.city.find({name:{$nin:["Bangalore"]}})
**
- Edit
**
> var result = []; //Define an empty Array
> var cur = db.city.aggregate([{$match:{name:{$in:["Bangalore","Delhi"]}}}]); //Get matching city and save it in cursor
> while(cur.hasNext()){ result.push(cur.next()) } // Retrieve the cursor data and push it in array
> var cur = db.city.aggregate([{$match:{name:{$nin:["Bangalore","Delhi"]}}}]); // Get non matching city and save it in cursor (should be new variable)
> while(cur.hasNext()){ result.push(cur.next()) }
> for(var i=0;i<result.length;i++){printjson(result[i])}
This will give you expected result. Hope you are expecting the same :) https://stackoverflow.com/questions/32753750
复制相似问题