쿼리문은 Django, Spring 등 CRUD 작업과정에 많이 사용된다. Node.js에서도 쿼리문 제공을 통해서 원활한 CRUD가 진행될 수 있도록 하는데 한번 알아보자.
await Character.deleteMany({name: /Stark/, age: {$get: 18}});
await Character.deleteOne({name: 'Eddard Stark'});
1. await MyModel.find({});
2. await MyModel.find({name: 'john', age: {$gte: 18}}).exec();
3. MyModel.find({name: 'john', age: {$gte: 18}}, function(err, docs) {});
4. await MyModel.find({name: /john/i}, 'name friends').exec();
5. await MyModel.find({name: /john/i}, null, {skip: 10}).exec()
await Adventure.findById(id).exec();
Adventure.findById(id, function (err, adventure) {});
await Adventure.findById(id, 'name length').exec();
Model.findByIdAndDelete(id, options, callback);
Model.findByIdAndRemove(id, options, callback) // executes
Model.findByIdAndRemove(id, options) // return Query
Model.findByIdAndRemove(id, callback) // executes
Model.findByIdAndRemove(id) // returns Query
Model.findByIdAndRemove() // returns Query
Options
A.findByIdAndUpdate(id, update, options, callback) // executes
A.findByIdAndUpdate(id, update, options) // returns Query
A.findByIdAndUpdate(id, update, callback) // executes
A.findByIdAndUpdate(id, update) // returns Query
A.findByIdAndUpdate() // returns Query
Model.findByIdAndUpdate(id, {name: 'jason bourne'}, options, callback)
Model.findByIdAndUpdate(id, {$set: {name: 'jason bourne'}}, options, callback)
Options
// 조건에 맞는 것만 찾고 나머지는 null 값 처리함
await Adventure.findOne({country: 'Croatia'}).exec();
// callback 사용하는 경우
Adventure.findOne({country: 'Croatia'}, function (err, adventure) {});
// select only the adventures name and length
await Adventure.findOne({country: 'Croatia'}, 'name length').exec();
A.findOneAndDelete(conditions, options, callback) // executes
A.findOneAndDelete(conditions, options) // return Query
A.findOneAndDelete(conditions, callback) // executes
A.findOneAndDelete(conditions) // returns Query
A.findOneAndDelete() // returns Query
A.findOneAndRemove(conditions, options, callback) // executes
A.findOneAndRemove(conditions, options) // return Query
A.findOneAndRemove(conditions, callback) // executes
A.findOneAndRemove(conditions) // returns Query
A.findOneAndRemove() // returns Query
A.findOneAndReplace(conditions, options, callback) // executes
A.findOneAndReplace(conditions, options) // return Query
A.findOneAndReplace(conditions, callback) // executes
A.findOneAndReplace(conditions) // returns Query
A.findOneAndReplace() // returns Query
findOneAndDelete/findOneAndRemove/findOneAndReplace의 Options는 아래와 같다.
Options
- sort : 조건에 맞는 다양한 document가 발견될 경우, 정렬 순서를 정함
- maxTimeMS : 쿼리문 작동 시간을 제한함
- select : document를 return으로 세팅함
- projection : select와 동일함
- rawResult : true이면 MongoDB에서 raw한 결과를 반환함
- strict : 업데이트를 통해 스키마의 strict mode로 덮어씀
A.findOneAndUpdate(conditions, update, options, callback) // executes
A.findOneAndUpdate(conditions, update, options) // returns Query
A.findOneAndUpdate(conditions, update, callback) // executes
A.findOneAndUpdate(conditions, update) // returns Query
A.findOneAndUpdate() // returns Query
const query = { name: 'borne' };
Model.findOneAndUpdate(query, { name: 'jason bourne' }, options, callback)
Model.findOneAndUpdate(query, { $set: { name: 'jason bourne' }}, options, callback)
Options
업데이트와 비슷하지만 MongoDB를 제외하고, 주어진 document와 현재 document를 대체한다.
const res = await Person.replaceOne({_id: 24601}, {name: 'Jean Valjean'});
res.n; // 매칭된 document 수
res.nModified;
업데이트와 비슷하지만 MongoDB를 제외하고, multi 옵션 값과 관계 없이 filter를 매칭하는 모든 document를 업데이트 함
const res = await Person.updateMany({name: /Stark$/}, {isDeleted: true});
res.n; // 매칭된 document 수
res.nModified; // 수정된 document 수
update와 비슷하지만 multi/overwrite 옵션 지원 없이 매칭된 첫 번째 document를 업데이트함
const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
res.n; // 매칭된 document 수
res.nModified; // 수정된 document 수