LoginSignup
0
0

Nodeのmongodbでfindにprojection指定

Posted at

バージョン
mongodb@3.7.4

.js
// findOneの場合
db.collection('collection').findOne({ key: 'value' }, { projection: { _id: 0 } });

// findの場合
db.collection('collection').find({ key: 'value' }, { projection: { _id: 0 } }).toArray();
db.collection('collection').find({ key: 'value' }).project({ _id: 0 }).toArray();

この書き方で「_id」だけ取得対象外にできます。

ちなみに

.js
db.collection('collection').findOne({ key: 'value' }).project({ _id: 0 });

これだと「project is not a function」と言われてエラーになります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0