0

My first adventure into Mongo. Please save me some time by answering the following. This is the schema.

"_id" : 1,
  "FullName" : "Full Name",
  "Email" : "[email protected]",
  "FacebookId" : NumberLong(0),
  "LastModified" : ISODate("2012-04-11T09:26:10.955Z"),
  "Connections" : [{
      "_id" : 7,
      "FullName" : "Fuller name",
      "Email" : "[email protected]",
      "FacebookId" : NumberLong(0),
      "LastModified" : ISODate("0001-01-01T00:00:00Z")
    },

  ....

Given an id of a single top user, i'd like to return all of the Emails in the Connections array, and preferably, just the emails. What's the querystring? Much obliged!

1 Answer 1

1

You can't get only values from the sub-objects in MongoDB.

If you do a query like this:

db.test.find({"_id": 1}, {"Connections.Email":1});

you will get this kind of response:

{ 
  "_id": 1, 
  "Connections" : [ {"Email":"[email protected]"}, 
                    {"Email":"[email protected]"} ] 
}

This is the closest you can get with a simple query and field selection from MongoDB.

You can then filter out the e-mails values in your code with a simple foreach.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.