batchSize field name ignored in Field Projection
You are correct that the driver incorrectly interprets this as the batchSize
option and ignores the projection statement.
The correct way to do this though in modern driver releases is to actually use the .project()
"cursor method" instead. This is more consistent with other language driver implementations.
db.collection('collection').find()
.project({ name: 1, batchSize: 1})
.toArray();
As a full demonstration:
const mongodb = require('mongodb'),
MongoClient = mongodb.MongoClient;
(async function() {
let db;
try {
db = await MongoClient.connect('mongodb://localhost/test');
// New form uses .project() as a cursor method
let result = await db.collection('collection').find()
.project({ name: 1, batchSize: 1})
.toArray();
console.log(JSON.stringify(result,undefined,2));
// Legacy form confuses this as being a legacy "cursor option"
let other = await db.collection('collection')
.find({},{ name: 1, batchSize: 1 })
.toArray();
console.log(JSON.stringify(other,undefined,2));
} catch(e) {
console.error(e)
} finally {
db.close()
}
})()
Produces the output:
[
{
"_id": "594baf96256597ec035df23c",
"name": "Batch 1",
"batchSize": 30
},
{
"_id": "594baf96256597ec035df234",
"name": "Batch 2",
"batchSize": 50
}
]
[
{
"_id": "594baf96256597ec035df23c",
"name": "Batch 1",
"batchSize": 30,
"users": []
},
{
"_id": "594baf96256597ec035df234",
"name": "Batch 2",
"batchSize": 50,
"users": []
}
]
Where the first output form is the corrected one, using .project()
projection query in mongodb
new MongoDB nodeJS driver is not supporting this anymore, you could check the docs
for mongoDB versions > 3.4, you need to use the method .project()
so your query will be something like that
db.collection('users').findOne({ username : decoded.username })
.project({ password: 0 }).then(user => {
console.log(user)
});
MongoDB returns the id even if I tell it not to
Answer found by Joe with this solution : https://stackoverflow.com/a/62301374/10891826
I had to modify the { _id: 0 }
into { projection: { _id: 0 } }
.
collectionPoints.find({ points: { $gt: -1 } }, { projection: { _id: 0 } }).toArray();
node mongodb 3.4 using find with specify fields and toArray does not filter results
Use .project
cursor method instead
db.collection('collection')
.find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
.project({ '_id': 0, 'value.someCategory': 1 })
.toArray()
Related Topics
How to Filter by Object Property in Angularjs
What Is the Meaning of an Underscore in JavaScript Function Parameter
What Does Document.Domain = Document.Domain Do
How to Remove an Object from an Array with JavaScript
Passing in Dynamic Key:Value Pairs to an Object Literal
Random Number Generator Without Dupes in JavaScript
Jquery Get Values of Checked Checkboxes into Array
How to Verify Jquery Ajax Events with Jasmine
Jest: Timer and Promise Don't Work Well. (Settimeout and Async Function)
Getting How Many People Are in a Chat Room in Socket.Io
React - How to Force a Function Component to Render
Get Selected Text Position and Place an Element Next to It
Node-Style Require for In-Browser JavaScript
How to Detect Responsive Breakpoints of Twitter Bootstrap 3 Using JavaScript