Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-3276

Mentioning the different syntax when use .projection() for find() vs findOne()

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Docs

      Hello,

       

      I'm not sure if it's the right place to open a issue about the documentation.

      For the NodeJs driver documentation in the section:

      CRUD Operations > Read Operations > Specify Which Fields To Return 

      To control which fields appear in the documents return by the read operations, we need to use projection:

       

      const projection = { name: 1 }

      ;

      const cursor = collection.find().project(projection);

       

      However this syntax doesn't work when using findOne() function. 

       

      Based on this Stack Overflow post

      If we want to combine findOne() with .project() we need to use the following syntax:

      const projection = { name: 1 }

      ;

      const cursor = collection.findOne({email}, projection: {email: 1})

       
       
      I would like to know if it's possible to mention in the documentation that:
       
      find() return a cursor, while the findOne() return the direct result, maybe an ordinary document object, which obviously don't have a .project() attribute. 
       
      It will hopefully save time to the next developer 

            Assignee:
            Unassigned Unassigned
            Reporter:
            steven.loofat@gmail.com Steven Loo Fat
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: