Мангуст и sql сравнение - PullRequest
       5

Мангуст и sql сравнение

0 голосов
/ 28 августа 2018

Это много вопросов

Из результата Мангуст у меня есть несколько предметов, которые я получаю через 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

1012 *, например *

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

Как я могу сделать это в мангусте?

1 Ответ

0 голосов
/ 28 августа 2018
select * 
from Person 
where first_name in (Smith, Queen)

Было бы просто использовать $ в :

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'

Используя mongoose, вам нужно будет использовать заполнить и создать схемы с 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() ...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...