I'm trying to use a projection parameter on findOne() to extract a single field from a document (stats) but it just seems to return the whole document. I'm using version "mongodb": "^3.4.1" in Node.js
This is the document structure
{ _id: 5e563015fa9a1a0134cac3cb,
username: 'user1',
password: '1234',
email: '[email protected]',
stats:
{ totalViewed: 122,
totalUnique: 4,
tknow: 80,
tdknow: 42,
setCnt: 78 },
progress:
[ { cardId: 1001, knowCnt: 3, dknowCnt: 4 },
{ cardId: 1016, knowCnt: 0, dknowCnt: 0 } ] }
This is the code:
var findOneDoc = function() {
db.collection("testusers").findOne(
{ username: "user1" },
{ stats: 1 }, //field to return
function(err, result) {
if (err) {
console.log("Error: ", err);
}
console.log("Success: ", result);
}
);
};
findOneDoc();
I also tried:{$project: {stats: 1}}
, to no avail
Thanks
MongoDB findOne() method returns only one document that satisfies the criteria entered. If the criteria entered matches for more than one document, the method returns only one document according to natural ordering, which reflects the order in which the documents are stored in the database.
It looks like findOne() is only deprecated in the Javascript driver.
The findOne() method finds and returns one document that matches the given selection criteria. If multiple documents satisfy the given query expression, then this method will return the first document according to the natural order which reflects the order of documents on the disk.
If you don’t want the _id field, then set _id:0 in the projection. The first parameter is a query criteria on the collection. Other is optional. projection: The projection parameter determines which fields are returned to the matching documents. The projection parameter takes a document that contains field : value pairs:
By default, MongoDB always includes the _id field in the returned documents. To suppress it, you need to explicitly specify _id: 0 in the projection argument.
This parameter is basically used to specify the criteria of the query using the query operator in MongoDB. Projection: This type of parameter is basically used to specify the return of the field using operators in MongoDB. If we have not used this parameter then it will return all the fields from the matching document.
Summary: in this tutorial, you’ll how to use the MongoDB projection that allows you to select fields to return from a query In MongoDB, projection simply means selecting fields to return from a query. By default, the find () and findOne () methods return all fields in matching documents. Most of the time you don’t need data from all the fields.
Based on the documentation the .findOne()
method takes options as a second parameter and it is recommended to use projection
to define fields:
db.collection("testusers").findOne(
{ username: "user1" },
{ projection: { stats: 1 } },
function(err, result) { ... }
);
with findOne
operations, the second parameter passed is an options parameter, so you should pass in your projects within the options parameter, like this:
query = { username: "user1" };
options = { projection: { stats: 1 }};
db.collection("testusers").findOne(query, options)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With