Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB projection parameter not working in findOne()

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

like image 425
quietplace Avatar asked Mar 05 '20 01:03

quietplace


People also ask

How does findOne work in MongoDB?

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.

Is findOne deprecated?

It looks like findOne() is only deprecated in the Javascript driver.

What does findOne return MongoDB?

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.

What is the projection parameter in MongoDB?

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:

How to suppress the_ID field in MongoDB projections?

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.

What is the use of query parameter in MongoDB?

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.

How to select fields from a query in MongoDB?

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.


2 Answers

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) { ... }
);
like image 59
mickl Avatar answered Oct 12 '22 02:10

mickl


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)
like image 33
Bryant Avatar answered Oct 12 '22 02:10

Bryant