question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NodeJS SDK $project syntax? (mongo query projections)

See original GitHub issue

Goals

(copied from SO post https://stackoverflow.com/questions/63942993/mongodb-realm-nodejs-sdk-project-syntax)

I’m trying to do a simple query where some collection data is returned. I want to filter out the _id field in the results.

From my understanding (based on the documentation) the syntax to do so should look like this:

myCollection.findOne(
        { name: hostName },
        { $project: { _id: 0 } }
)

However when I do this the filter has not been applied, e.g.:

{
  _id: XXXXXXXXXXXXXXXXXXXX,
  name: 'the name',
  ...
}

Can anybody point me in the right direction?

Expected Results

Receiving a filtered query result

Actual Results

The entire query was returned.

Steps to Reproduce

Use the sample code above in your own database. Try to use a projection to filter a query.

Version of Realm and Tooling

  • Realm JS SDK Version: 10.0.0-beta.12
  • Node or React Native: Node

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
knethcommented, Oct 1, 2020

@johnsaigle If you go to https://docs.mongodb.com/realm/node/mongodb/, you can leave a comment by clicking “No” to “Was this page useful?”. The docs team will then be notified.

1reaction
steffenaggercommented, Sep 29, 2020

Hello @johnsaigle, I completely understand your confusion - it seems the docs & SDK are not currently aligned. I believe it’s projection instead of $project, as:

myCollection.findOne(
  { name: 'find me' },
  { projection: { _id: 0 } }
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Specify Which Fields to Return — Node.js - MongoDB
Use a projection to control which fields appear in the documents returned by read operations. Many requests only require certain fields, so projections...
Read more >
MongoDB projection syntax - node.js - Stack Overflow
Another behavior is that when I try to project the name on the mongo shell, the result is only the document _id ....
Read more >
CRUD with the MongoDB Node.js SDK - Retool
Get help navigating selectOnes, understanding projections, figuring out the differences between insertMany and bulkWrite, and funky SQL ...
Read more >
MongoDB Projection & Projection Operators Explained
MongoDB projection uses the same find syntax, but we also add a set of parameters to the find function. This set of parameters...
Read more >
MongoDB findOne Example - DigitalOcean
MongoDB findOne() syntax is: db.collection.findOne(<criteria>, <projection>) criteria - specifies the selection criteria entered. projection ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found