如何使用 mongoose(express.js) 为 select 数据写入条件?
How to write condtion for select data using mongoose(express.js)?
例如我给了 5 个对象,这是移动卖家列表。我想 select 只有谁都在卖 Brand:Nokia,型号名称:Lumia,子型号:Lumia 735 TS,Lumia 510 怎么能我在 express 中写条件,如果用户 select Brand:Nokia,型号名称:Lumia,子型号:Lumia 735 TS 我如何在 express 中写条件我已经给了我的控制器请帮助我摆脱过去 3 天的挣扎。
mongodb
中存储的卖家列表
object 1
{
"_id": {
"$oid": "56a63a968411612f36ab4b5d"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "7500",
"name": "Lumia 735 TS"
},
{
"price": "1000",
"name": "Lumia 510"
}
],
"name": "Lumia"
}
],
"brandname": "Nokia",
"id": "986745"
}
],
"Storename": "Poorvika Mobiles",
"__v": 0
}
object 2:
{
"_id": {
"$oid": "56a63b418411612f36ab4b5e"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "7500",
"name": "Lumia 735 TS"
},
{
"price": "8500",
"name": "Lumia 830"
}
],
"name": "Lumia"
}
],
"brandname": "Nokia",
"id": "986745"
}
],
"Storename": "Nokia Store",
"__v": 0
}
object 3:
{
"_id": {
"$oid": "56a63b738411612f36ab4b5f"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "7500",
"name": "Lumia 735 TS"
},
{
"price": "8500",
"name": "Lumia 830"
}
],
"name": "Lumia"
},
{
"submodel": [
{
"price": "12000",
"name": "Asha Asn01"
},
{
"price": "5000",
"name": "Nokia Asha Dual sim"
}
],
"name": "Asha"
}
],
"brandname": "Nokia",
"id": "986745"
}
],
"Storename": "Sun Mobiles",
"__v": 0
}
object 4:
{
"_id": {
"$oid": "56a63b7d8411612f36ab4b60"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "7500",
"name": "Lumia 735 TS"
},
{
"price": "1000",
"name": "Lumia 510"
}
],
"name": "Lumia"
},
{
"submodel": [
{
"price": "12000",
"name": "Asha Asn01"
},
{
"price": "5000",
"name": "Nokia Asha Dual sim"
}
],
"name": "Asha"
}
],
"brandname": "Nokia",
"id": "986745"
}
],
"Storename": "Mobile Corner",
"__v": 0
}
object 5:
{
"_id": {
"$oid": "56a63b948411612f36ab4b61"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "8500",
"name": "Trend 840"
},
{
"price": "10000",
"name": "Galaxy Note Duos"
}
],
"name": "Galaxy"
}
],
"brandname": "Samsung",
"id": "144745"
}
],
"Storename": "Asath Mobile Center",
"__v": 0
}
object 6:
{
"_id": {
"$oid": "56a63ba48411612f36ab4b62"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "8500",
"name": "Trend 840"
},
{
"price": "10000",
"name": "Galaxy Note Duos"
}
],
"name": "Galaxy"
},
{
"submodel": [
{
"price": "6700",
"name": "Asha 230"
},
{
"price": "8500",
"name": "Asha Asn01"
}
],
"name": "Asha"
}
],
"brandname": "Samsung",
"id": "144745"
}
],
"Storename": "Zigzag Mobiles",
"__v": 0
}
object 7:
{
"_id": {
"$oid": "56a63baf8411612f36ab4b63"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "7500",
"name": "Lumia 735 TS"
},
{
"price": "1000",
"name": "Lumia 510"
}
],
"name": "Lumia"
},
{
"submodel": [
{
"price": "12000",
"name": "Asha Asn01"
},
{
"price": "5000",
"name": "Nokia Asha Dual sim"
}
],
"name": "Asha"
}
],
"brandname": "Nokia",
"id": "986745"
},
{
"models": [
{
"submodel": [
{
"price": "8500",
"name": "Trend 840"
},
{
"price": "10000",
"name": "Galaxy Note Duos"
}
],
"name": "Galaxy"
},
{
"submodel": [
{
"price": "6700",
"name": "Asha 230"
},
{
"price": "8500",
"name": "Asha Asn01"
}
],
"name": "Asha"
}
],
"brandname": "Samsung",
"id": "144745"
}
],
"Storename": "Mobile Bazar",
"__v": 0
}
控制器:
exports.shopByPhone = function(req, res) {
Shop.find().sort('-created').populate('user', 'displayName').exec(function(err, shops) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(shops);
}
});
};
条件一:
我要筛选只卖的数据
品牌:诺基亚
型号:Lumia
子型号:Lumia 735 TS、Lumia 510
如果以上条件匹配,它应该只显示这 3 个卖家
1.Poorvika 手机
2.Mobile角
3.Mobile巴扎尔
这3家店铺只卖
品牌:诺基亚
型号:Lumia
子型号:Lumia 735 TS、Lumia 510
条件 2:
我要过滤只卖的数据
品牌:诺基亚
型号:Lumia
子型号:Lumia 735 TS
如果以上条件匹配,它应该只显示这 3 个卖家
1.Poorvika 手机
2.Mobile角
3.Mobile巴扎尔
这3家店铺只卖
品牌:诺基亚
型号:Lumia
子型号:Lumia 735 TS、Lumia 510
尝试
find({
'Categories.brandname':'Nokia',
'Categories.models.name':'Lumia',
'Categories.models.submodel.name':
{'$all':['Lumia 510', 'Lumia 735 TS']}
})
$all
将为您提供销售 Lumia 510 和 Lumia 735 的商店。如果您需要选择 两者之一的查询 这些型号中,请改用 $in
。
例如我给了 5 个对象,这是移动卖家列表。我想 select 只有谁都在卖 Brand:Nokia,型号名称:Lumia,子型号:Lumia 735 TS,Lumia 510 怎么能我在 express 中写条件,如果用户 select Brand:Nokia,型号名称:Lumia,子型号:Lumia 735 TS 我如何在 express 中写条件我已经给了我的控制器请帮助我摆脱过去 3 天的挣扎。 mongodb
中存储的卖家列表object 1
{
"_id": {
"$oid": "56a63a968411612f36ab4b5d"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "7500",
"name": "Lumia 735 TS"
},
{
"price": "1000",
"name": "Lumia 510"
}
],
"name": "Lumia"
}
],
"brandname": "Nokia",
"id": "986745"
}
],
"Storename": "Poorvika Mobiles",
"__v": 0
}
object 2:
{
"_id": {
"$oid": "56a63b418411612f36ab4b5e"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "7500",
"name": "Lumia 735 TS"
},
{
"price": "8500",
"name": "Lumia 830"
}
],
"name": "Lumia"
}
],
"brandname": "Nokia",
"id": "986745"
}
],
"Storename": "Nokia Store",
"__v": 0
}
object 3:
{
"_id": {
"$oid": "56a63b738411612f36ab4b5f"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "7500",
"name": "Lumia 735 TS"
},
{
"price": "8500",
"name": "Lumia 830"
}
],
"name": "Lumia"
},
{
"submodel": [
{
"price": "12000",
"name": "Asha Asn01"
},
{
"price": "5000",
"name": "Nokia Asha Dual sim"
}
],
"name": "Asha"
}
],
"brandname": "Nokia",
"id": "986745"
}
],
"Storename": "Sun Mobiles",
"__v": 0
}
object 4:
{
"_id": {
"$oid": "56a63b7d8411612f36ab4b60"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "7500",
"name": "Lumia 735 TS"
},
{
"price": "1000",
"name": "Lumia 510"
}
],
"name": "Lumia"
},
{
"submodel": [
{
"price": "12000",
"name": "Asha Asn01"
},
{
"price": "5000",
"name": "Nokia Asha Dual sim"
}
],
"name": "Asha"
}
],
"brandname": "Nokia",
"id": "986745"
}
],
"Storename": "Mobile Corner",
"__v": 0
}
object 5:
{
"_id": {
"$oid": "56a63b948411612f36ab4b61"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "8500",
"name": "Trend 840"
},
{
"price": "10000",
"name": "Galaxy Note Duos"
}
],
"name": "Galaxy"
}
],
"brandname": "Samsung",
"id": "144745"
}
],
"Storename": "Asath Mobile Center",
"__v": 0
}
object 6:
{
"_id": {
"$oid": "56a63ba48411612f36ab4b62"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "8500",
"name": "Trend 840"
},
{
"price": "10000",
"name": "Galaxy Note Duos"
}
],
"name": "Galaxy"
},
{
"submodel": [
{
"price": "6700",
"name": "Asha 230"
},
{
"price": "8500",
"name": "Asha Asn01"
}
],
"name": "Asha"
}
],
"brandname": "Samsung",
"id": "144745"
}
],
"Storename": "Zigzag Mobiles",
"__v": 0
}
object 7:
{
"_id": {
"$oid": "56a63baf8411612f36ab4b63"
},
"default": [],
"Categories": [
{
"models": [
{
"submodel": [
{
"price": "7500",
"name": "Lumia 735 TS"
},
{
"price": "1000",
"name": "Lumia 510"
}
],
"name": "Lumia"
},
{
"submodel": [
{
"price": "12000",
"name": "Asha Asn01"
},
{
"price": "5000",
"name": "Nokia Asha Dual sim"
}
],
"name": "Asha"
}
],
"brandname": "Nokia",
"id": "986745"
},
{
"models": [
{
"submodel": [
{
"price": "8500",
"name": "Trend 840"
},
{
"price": "10000",
"name": "Galaxy Note Duos"
}
],
"name": "Galaxy"
},
{
"submodel": [
{
"price": "6700",
"name": "Asha 230"
},
{
"price": "8500",
"name": "Asha Asn01"
}
],
"name": "Asha"
}
],
"brandname": "Samsung",
"id": "144745"
}
],
"Storename": "Mobile Bazar",
"__v": 0
}
控制器:
exports.shopByPhone = function(req, res) {
Shop.find().sort('-created').populate('user', 'displayName').exec(function(err, shops) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(shops);
}
});
};
条件一: 我要筛选只卖的数据
品牌:诺基亚
型号:Lumia
子型号:Lumia 735 TS、Lumia 510
如果以上条件匹配,它应该只显示这 3 个卖家
1.Poorvika 手机
2.Mobile角
3.Mobile巴扎尔
这3家店铺只卖
品牌:诺基亚
型号:Lumia
子型号:Lumia 735 TS、Lumia 510
条件 2:
我要过滤只卖的数据
品牌:诺基亚
型号:Lumia
子型号:Lumia 735 TS
如果以上条件匹配,它应该只显示这 3 个卖家
1.Poorvika 手机
2.Mobile角
3.Mobile巴扎尔
这3家店铺只卖
品牌:诺基亚
型号:Lumia
子型号:Lumia 735 TS、Lumia 510
尝试
find({
'Categories.brandname':'Nokia',
'Categories.models.name':'Lumia',
'Categories.models.submodel.name':
{'$all':['Lumia 510', 'Lumia 735 TS']}
})
$all
将为您提供销售 Lumia 510 和 Lumia 735 的商店。如果您需要选择 两者之一的查询 这些型号中,请改用 $in
。