Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

73
Views
Return only one field from MongoDB query

Suppose my document looks like this

{
    "_id": 2,
    "Name": "Abcd",
    "Connected": [1,3]
}

Now, I want to retrieve the array [1,3] via the _id or Name. The purpose is to save this array into a JavaScript array.

Hence I tried using db.myCollection.find({"_id":2},{"Connected":1}) but it return a document again moreover with _id still there like this

{ "_id" : 2, "Connected" : [ 1, 3 ] }

How can I retrieve only that array?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can optionally remove the ID from your result, by setting it to 0 in the projection:

db.myCollection.find({"_id":2},{"Connected":1, "_id":0})

The ID field is the only field that can be excluded in projections while other fields are included.

If you want to get the Connected field directly, you can use findOne:

db.myCollection.findOne({"_id":2}, {"Connected":1, "_id":0}).Connected
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!