programing

Mongoose에서 ID 배열 쿼리를 수행하려면 어떻게 해야 합니까?

javajsp 2023. 5. 24. 21:43

Mongoose에서 ID 배열 쿼리를 수행하려면 어떻게 해야 합니까?

예를 들어 모델 이름이User개체 ID가 있는 배열이 있습니다.

제가 가지고 있는 ID 배열과 "교차"하는 모든 사용자 레코드를 가져오려고 합니다.

User.find({ records with IDS IN [3225, 623423, 6645345] }, function....

여기 $in 연산자를 사용하는 몽구스식 방법이 있습니다.

User.find()
  .where('fb.id')
  .in([3225, 623423, 6645345])
  .exec(function (err, records) {
    //make magic happen
  });

점 표기법은 하위 문서를 조회하기에 매우 편리합니다.

http://mongoosejs.com/docs/queries.html

$in 연산자를 사용해야 합니다 >

https://docs.mongodb.com/manual/reference/operator/query/in/ #op._S_in

예:

Users.find( { "fb" : { id: { $in : arrayOfIds } } }, callback );
User.where({ records: { $in: [3225, 623423, 6645345] } }, function ...

자세한 내용은 http://docs.mongodb.org/manual/reference/operator/query/ 에서 확인하십시오.

저는 이런 식으로 일하세요.

IDs=["5b00c4b56c7fb80918293dd9","5b00c4b56c7fb80918293dd7",...]
const users= await User.find({records:IDs}) 

ID는 개체 ID의 배열입니다.

const ids =  [
    '4ed3ede8844f0f351100000c',
    '4ed3f117a844e0471100000d', 
    '4ed3f18132f50c491100000e',
];

콜백 포함:

User.find().where('_id').in(ids).exec(callback);

비동기 기능 포함:

records = await User.find().where('_id').in(ids).exec();

언급URL : https://stackoverflow.com/questions/5818303/how-do-i-perform-an-id-array-query-in-mongoose