简体   繁体   中英

Text search with projection in MongoDB & Node.js

Is it possible to do a text search and apply projection to the returned documents via the Node.js API in MongoDB? The code below does not apply the projection and I get the entire document back, however I am only after the person property.

collection.find({ $text: { $search: 'hello' } }, { person: 1 }).toArray();

Have you tried to use project method like this:

db.collection('collectionName').find({ $text: { $search: 'hello' } }).project({ person: 1 });

As stated in documentation this should be way how you can do projection with MongoDB Node.js library. The example you have provided is how can you do projection in Mongo Shell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM