Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:55:40+00:00 2026-06-13T19:55:40+00:00

In MongoDB, To query for records that contain certain fields you can do: collection.find({‘field_name1’:

  • 0

In MongoDB,

To query for records that contain certain fields you can do:

collection.find({'field_name1': {'$exists': true}})

And that will return any record that has the ‘field_name1’ field set…

But how do you query mongo to find records that contains ONLY ‘field_name1’ (and no other fields)? I’d like to be able to do this for, say, a list of fields.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:55:41+00:00Added an answer on June 13, 2026 at 7:55 pm

    The sad answer, as you’ll often find with MongoDB and other NoSQL databases is probably that it would be best to structure your data in a way that allows you to query it as simply as possible.

    That said, there are ways of doing this, but as far as I know, it requires you execute JavaScript server side. This will be slow, and cannot possibly take advantage of indexes and other logical features of MongoDB, so use it only if it’s absolutely necessary, if performance is at all important.

    So, the easiest way to do this, is probably to create a function that returns the number of fields in an object, which we can use with the $where query syntax. This allows you to run arbitrary JavaScript queries against your data, and can be combined with normal syntax queries.

    Sadly, my JavaScript-fu is a little weak, so I don’t know how (or if) you can get at the count of members of an object in JS in a one-liner, so to do this, I would store a function server side.

    From the mongo shell, execute the following:

    db.system.js.save(
        {
            "_id" : "countFields", 
            "value" : function(x) { i=0; for(p in x) { i++; } return i}
        }
    )
    

    With that, you have a saved JavaScript function, server side, called countFields that returns the number of elements in an object. Now, you need to execute your find-operation with the $where query:

    db.collection.find({
        'field_name1': {'$exists': True}, 
        '$where' : 'countFields(this)==2'
    })
    

    This would give you only the documents that meet both the $exists condition, and the $where clause. Note that I’m comparing with 2 in the example, since the countFields function counts _id as a field.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran the following query on a collection in my mongodb database. db.coll.find({field_name:{$exists:true}}).count() and
I'm looking to perform a REGEX on a MongoDB find query that only finds
How can I do a query in Mongodb to find the count of entry
Does MongoDB offer a find or query method to test if an item exists
If I perform the following query in MongoDB shell, all is fine: db.users.find({location: {$near:
I have this query in MongoDB db.privateMessages.find( { $or : [ {fromId: userId, toId:
In MongoDB I have a query which looks like this to find out for
Coll.update(condition, new BasicDBObject($set, data), true, true)) This is my update query in mongodb for
So, MongoDB defaults to AND when finding records. For example: db.users.find({age: {'$gte': 30}, {'$lte':
I have a MongoDB query that's taking an unreasonably long time to run, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.