Ticket: Projection and the word projection with find

Why does this not work in the test?

  cursor = await movies.find(
    {countries: {$in: countries }},
    {title: 1}
  )

But the following does work:

  cursor = await movies.find(
    {countries: {$in: countries }},
    { projection: {title: 1}}
  )

I’m confused. The docs explain how to use projection with find here https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection-examples

db.bios.find( { }, { name: 1, contribs: 1 } )

Projection takes the following form:

{ <field1>: <value>, <field2>: <value> ... }

I’m use to the above in mongoose as that works there too. But i don’t see in the docs the requirement to use the word projection like the second example.

What specifically and where did i miss this in the docs?

Using projection like { projection: {<key>: <value>}} is not in here either. Yet we have to use that syntax with the mflix-js? Why?

Hi @Travis_Lindsey,

I used below code in my mflix-js/src/dao/moviesDAO.js file and its working for me:

cursor = await movies.find({<REPLACE FILTER>}},

        ).project({abc: 1})

Hello @kanikasingla,

You have a syntax error in your code. There should be an argument after your comma or the comma should be removed. You also have a extra curly brace before the comma.

The question is about why the official documented method from mongodb docs as linked in the original post does not work, where are the missing docs showing mongodb expecting projection.

Hi @Travis_Lindsey,

I used below code in my mflix-js/src/dao/moviesDAO.js file and its working for me:

cursor = await movies.find({<REPLACE FILTER>}},

        ).project({abc: 1})

Thanks for catching this.

I would recommend posting this in community forums or there is help section on the documentation where you can query the same :slight_smile: I think that would be the best and quick solution for now.

cc @Shubham_Ranjan

Kanika

Thanks for the idea to post in the mongodb main community forum. Should I post there even though the issue is course specific only?

My queries using the official documented methods work in all my projects. The only problem is they didn’t work in the mongodb university course running test in mflix-js and the working method in the course isn’t documented anywhere.

Hi @Travis_Lindsey,

This is a mongo shell method. They syntax for drivers are slightly different.

You can see here in the docs that the find command takes two arguments : find(query, options)

Under options you can specify other optional settings as a key-value pair.

Hope it helps!

~ Shubham

1 Like

@Shubham_Ranjan thank you!