这是一个多问题
从猫鼬的结果中,我可以通过result.length
获得结果中的几项。
我如何获得特定物品,例如
Person.find({'exployment.office':'Greenway'})
.exec(function(err, result){
//from the result, how can i get those items with
result.length would give me the number of items in the result
but how can i get a specific item without having to use loop to get
the specific item within the result
//There are like fifty people in the result,
//how can i get the number of items in result
});
在SQL中,我有这样的查询
select *
from table1, table2
where table2.field2 = value1
and table2.field2 = table1.field2
and table1.value1 = value3
例如
select *
from city, state
where state.name = 'xxy'
and state.id = city.state_id
and city.name != 'yyy'
如何将其转换为猫鼬?
在SQL中,如果我想选择名字例如为史密斯,女王,我可以使用类似的东西
select *
from table
where first_name in (Smith, Queen)
这将为我的名字与SMith和Queen相匹配的人提供结果
如何用猫鼬做到这一点?
答案 0 :(得分:1)
select *
from Person
where first_name in (Smith, Queen)
使用$in可以简单地做到这一点:
Person.find({'first_name': { $in: ['Smith', 'Queen']}}) ...
下一个:
select *
from city, state
where state.name = 'xxy'
and state.id = city.state_id
and city.name != 'yyy'
使用猫鼬,您将需要使用populate并创建具有ref关系的架构。
City.find({ "name": { $ne: "yyy"} }).populate({
"path": "state",
"match": { "state.name": "xxy" }
}) ...
下一个:
Person.find({'exployment.office':'Greenway'})
.exec(function(err, result){
//from the result, how can i get those items with
result.length would give me the number of items in the result
but how can i get a specific item without having to use loop to get
the specific item within the result
//There are like fifty people in the result,
//how can i get the number of items in result
});
在exec
查询之前,您将尽可能多地进行过滤以获取所需的记录,而不是在获得所有结果之后进行过滤。 result.length
肯定会给您计数,尽管您可以通过类似方法获得计数;
Person.count({first_name: "Smith"}).exec() ...