๐Ÿ’ก find์™€ findOne์˜ ์ฐจ์ด

Jake_Youngยท2021๋…„ 3์›” 19์ผ
2

๐Ÿ”ฅ ๋ฌธ์ œ ์ƒํ™ฉ


// find

// find all documents
await MyModel.find({});

// find all documents named john and at least 18
await MyModel.find({ name: 'john', age: { $gte: 18 } }).exec();

// executes, passing results to callback
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});

// executes, name LIKE john and only selecting the "name" and "friends" fields
await MyModel.find({ name: /john/i }, 'name friends').exec();

// passing options
await MyModel.find({ name: /john/i }, null, { skip: 10 }).exec();



// findOne

// Find one adventure whose `country` is 'Croatia', otherwise `null`
await Adventure.findOne({ country: 'Croatia' }).exec();

// using callback
Adventure.findOne({ country: 'Croatia' }, function (err, adventure) {});

// select only the adventures name and length
await Adventure.findOne({ country: 'Croatia' }, 'name length').exec();

๐ŸŒธ ์š”์•ฝ

  • ๊ทธ๋ž˜์„œ ๋ญ๊ฐ€ ๋‹ค๋ฅธ๋ฐ?
  • ์‰ฝ๊ฒŒ ๋งํ•ด์„œ find()๋Š” documents์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ ,
  • findOne()์€ documents ๊ฐ์ฒด ํ•˜๋‚˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ํ•˜๋‚˜๋Š” ๋ฆฌ์ŠคํŠธ, ํ•˜๋‚˜๋Š” ๊ฐ์ฒด ํ•˜๋‚˜
  • ์ด๊ฒŒ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์ด๋‹ค.
  • find()๋กœ ํ•˜๋‚˜๋ฅผ ์ฐพ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๋ผ๋ฉด,
  • ๊ทธ ๋ฐ˜ํ™˜๊ฐ’์˜ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์„ ์žŠ์ง€๋ง์ž!
profile
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ํŒŒ์ด์ฌ ๊ทธ๋ฆฌ๊ณ  ์ปดํ“จํ„ฐ์™€ ๋„คํŠธ์›Œํฌ

0๊ฐœ์˜ ๋Œ“๊ธ€