我有以下种类的收藏
{
"_id" : ObjectId("5bbb299f06229dddbaab553b"),
"phone" : "+38 (031) 231-23-21",
"date_call" : "2018-10-08",
"adress_delivery" : "1",
"quantity_concrete" : "1",
"state" : "200",
"comments" : "1",
"is_order" : "n",
"date_delivery" : "",
"quantity_orders" : "",
"summ_order" : "",
"profit" : "",
"id" : "0"
}
在终端中请求时
db.getCollection("customers").find(
{
"$and" : [
{
"date_call" : {
"$lte" : "2018-10-10"
}
},
{
"date_call" : {
"$gte" : "2018-09-24"
}
}
]
},
{
"phone" : 1,
"is_order" : 1
}
);
我得到了答案
{
"_id" : ObjectId("5bbb299f06229dddbaab553b"),
"phone" : "+38 (031) 231-23-21",
"is_order" : "n"
}
在nodeJS中请求相同的查询时
client.db(“mongoDB”).collection("customers").find(
{
"$and": [
{
"date_call": {
"$lte" : "2018-10-14"
}
},
{
"date_call": {
"$gte" : "2018-09-24"
}
}
]
},
{
“phone”: 1,
"is_order": 1
}
)
我得到一个完整的答案,而不是查询中设置的“电话”和“ is_order”。请告诉我如何只获取指定的数据?
答案 0 :(得分:0)
您可以通过这种方式使用project
功能
client.db("mongoDB").collection("customers").find(
{
"$and": [
{
"date_call": {
"$lte" : "2018-10-14"
}
},
{
"date_call": {
"$gte" : "2018-09-24"
}
}
]
})
.project(
{
"phone": 1,
"is_order": 1
})
.toArray((err, res) => {
//your code here
})
也基于@Astro评论传递{fields:{"phone": 1, "is_order":1}}
作为投影对象