MongoDB - How to find equals in collection and in embedded document - mongodb

Gurus - I'm stuck in a situation that I can't figure out how I can query from the following collection "spouse", which has embedded document "surname" and check for equality with "surname" of this document:
{
"_id" : ObjectId("50bd2bb4fcfc6066b7ef090d"),
"name" : "Gwendolyn",
"surname" : "Davis",
"birthyear" : 1978,
"spouse" : {
"name" : "Dennis",
"surname" : "Evans",
"birthyear" : 1969
},
I need to query:
Output data for all spouses with the same surnames (if the surname of
one of the spouses is not specified, assume that it coincides with the
name of another)
I tried something like this:
db.task.find( {"surname" : { "spouse.surname" : 1 }} )
but it failed)
PLEASE PLEASE Guide me how I can achieve this any example/sample? based on this will be really helpful :-)
Thanks a lot!

You have three options.
Use $where modifier:
db.task.find({$where: 'this.spouse.surname === this.surname'})
Update all your documents and add special flag. After that you will be able to query documents by this flag. It's faster then $where, but requires altering your data.
Use MapReduce. It's quite complicated, but it allows you to do nearly anything.

Related

MongoDB: Find an element in a document that contains an object with a subobject with an array

I'm getting puzzled more and more discovering how mongodb is overcomplicated and bad designed in the query writing, anyway I have this kind of document in a db with thousand of records:
db.messages.aggregate([{$limit: 1}]).pretty()
{
"_id" : ObjectId("4f16fc97d1e2d32371003f42"),
"body" : "Hey Gillette,\n\nThe heat rate is going to depend on the type of fuel and the construction \ndate of the unit. Unfortunately, most of that info is proprietary. \n\nChris Gaskill is the head of our fundamentals group and he might be able to \nsupply you with some of the guidelines.\n\n-Bass\n\n\n \n\tEnron North America Corp.\n\t\n\tFrom: Lisa Gillette 04/05/2001 02:31 PM\n\t\n\nTo: Eric Bass/HOU/ECT#ECT\ncc: \nSubject: Power Generation Question\n\nHey Bass,\n\nI have a question and I am hoping you can help me. I am wanting to compile a \nlist of all the different types of power plants and their respective heat \nrates to determine some sort of generation ratio.\n\ni.e. Coal 4 mmbtu = 1 MW\n Simple Cycle 11 mmbtu = 1 MW\n\nPlease let me know if you can help me or point me to someone who can. Just \nFYI...Bryan suggested that I call you so blame him as you curse me under your \nbreath right now.\n\nThanks,\nLisa\n\n",
"filename" : "1045.",
"headers" : {
"Content-Transfer-Encoding" : "7bit",
"Content-Type" : "text/plain; charset=us-ascii",
"Date" : ISODate("2001-04-05T14:45:00Z"),
"From" : "eric.bass#enron.com",
"Message-ID" : "<2106897.1075854772243.JavaMail.evans#thyme>",
"Mime-Version" : "1.0",
"Subject" : "Re: Power Generation Question",
"To" : [
"lisa.gillette#enron.com"
],
"X-FileName" : "ebass.nsf",
"X-Folder" : "\\Eric_Bass_Jun2001\\Notes Folders\\Sent",
"X-From" : "Eric Bass",
"X-Origin" : "Bass-E",
"X-To" : "Lisa Gillette",
"X-bcc" : "",
"X-cc" : ""
},
"mailbox" : "bass-e",
"subFolder" : "sent"
}
And I need to find records from address X to address Y.
I managed to catch the "From" records with
db.messages.find({"headers.From": "eric.bass#enron.com"}).pretty().count()
But I can't get the To records (and I Need to get both togheter).
To query the "To" field I've tried:
db.messages.find({headers: {$elemMatch :{ "To": "lisa.gillette#enron.com"}}})
But it returns nothing
What am I missing?
Thanks
$elemMatch - To use this operator we need to give the array element and the matching operator, here in your case it should be like
db.messages.find({"headers.To": {$elemMatch :{$eq:"lisa.gillette#enron.com"}}})
$elemMatch is optimal to use when we have multiple queries to given for the array elements. If we are specifying only a single condition in the $elemMatch expression, we don't need to use $elemMatch, instead we can use find
db.messages.find({"headers.To": "lisa.gillette#enron.com"});

MongoDB query to capitalise first letter in existing database

Here is the problem,
I want to capitalise the first letter of a name in my existing
database, just wanted to know if there is any query so that i can make
it possbile.
what i want -- in my database so many names is in unformatted ways.. like lucy, Sean, jon and so on. i want to make them in a formatted ways like. Lucy. Sean, Jon. Can anyone help me with this?
Thanks in advance.
it may not be the best solution.
the only hiccup in below suggestion is to get "3" of $substr:["$name1",1,3] dynamically.
but gives you a start?
db.toupper.aggregate([{$project:{name:{$concat:[{$toUpper:{$substr:["$name1",0,1]}},{$substr:["$name1",1,**3**]}]}}}])
below is the result
db.toupper.find()
"_id" : ObjectId("5767ca0badb381a5cc0d19cd"), "name1" : "lean" }
"_id" : ObjectId("5767ca3aadb381a5cc0d19ce"), "name1" : "lean" }
db.toupper.aggregate([{$project:{name:{$concat:[{$toUpper:{$substr:["$name1",0,1]}},{$substr:["$name1",1,3]}]}}}])
"_id" : ObjectId("5767ca0badb381a5cc0d19cd"), "name" : "Lean" }
"_id" : ObjectId("5767ca3aadb381a5cc0d19ce"), "name" : "Lean" }

Mongo db : query on nested json

Sample json object :
{ "_id" : ObjectId( "55887982498e2bef5a5f96db" ),
"a" : "x",
"q" : "null",
"p" : "",
"s" : "{\"f\":{\"b\":[\"I\"]},\"time\":\"fs\"}" }
need all documents where time = fs
My query :
{"s":{"time" : "fs"}}
above returns zero products but that is not true.
There are two problems here. First of all s is clearly a string so your query cannot work. You can use $regex as below but it won't be very efficient:
{s: {$regex: '"time"\:"fs"'}}
I would suggest converting s fields to proper documents. You can use JSON.parse to do it. Documents can be updated based on a current value using db.foo.find().snapshot().forEach. See this answer for details.
Second problem is your query is simply wrong. To match time field you should use dot notation:
{"s.time" : "fs"})

get all embedded documents in collection

I have contact documents which contain an embedded document "lead". So the data would look like this:
{
"_id" : ObjectId("54f8fa496d6163ad64010000"),
"name" : "teretyrrtuyytiuyi",
"email" : "rertytruyy#fdgjioj.com",
"fax" : "",
"birth_date" : null,
"phone" : "dfgdfhfgjg",
"phone_2" :
"hgjhkhjkljlj",
"lead" : { "_id" : ObjectId("54f8fa496d6163ad64020000"), "appointment status" : "dfhgfgjghjk" }
}
When there are many contacts, there will be many leads. I want to retrieve all the leads in the collection, without retrieving the owning contact. I tried the following but none seem to work:
db.lead.find()
db.contacts.find({ 'lead.$' : 1})
Any way to do this?
If that query makes sense for you, you should have probably used a different data structure. If your embedded document has an id, it is almost certainly supposed to be a first-level citizen instead.
You can work around this using the aggregation framework, but I'd consider that a hack that probably works around some more profound problem with your data model.
It's also not very elegant:
>
> db.contacts.aggregate({ $project : {
"appointment_status" : "$lead."appointment_status",
"lead_id" : "$lead.id", ... } });
>
That way, it'll look as if leads was a collection of its own, but it's not and this is just a bad hack around it.
Note that there's no wildcard operator, so if you want to have all fields projected to the root level, you'll have to do it manually. It'd be much easier to simply read the regular documents - if that's not what you need, correct your schema design.

In MongoDB, how does on get the value in a field for an embedded document, but query based on a different value

I have a basic structure like this:
> db.users.findOne()
{
"_id" : ObjectId("4f384903cd087c6f720066d7"),
"current_sign_in_at" : ISODate("2012-02-12T23:19:31Z"),
"current_sign_in_ip" : "127.0.0.1",
"email" : "something#gmail.com",
"encrypted_password" : "$2a$10$fu9B3M/.Gmi8qe7pXtVCPu94mBVC.gn5DzmQXH.g5snHT4AJSZYCu",
"last_sign_in_at" : ISODate("2012-02-12T23:19:31Z"),
"last_sign_in_ip" : "127.0.0.1",
"name" : "Trip Jameson",
"sign_in_count" : 100,
"usertimes" : [
...thousands and thousands of records like this one....
{
"enddate" : 348268392.115282,
"idle" : 0,
"startdate" : 348268382.116728,
"title" : "My Awesome Title"
},
]
}
So I want to find only usertimes for a single user where the title was "My Awesome Title", and then I want to see what the value for "idle" was in that record(s)
So far all I can figure out is that I can find the entire user record with a search like:
> db.users.find({'usertimes.title':"My Awesome Title"})
This just returns the entire User record though, which is useless for my purposes. Am I misunderstanding something?
Return only partial embedded documents is currently not supported by MongoDB
The matching User record will always be returned (at least with the current MongoDB version).
see this question for similar reference
Filtering embedded documents in MongoDB
This is the correspondent Jira on MongoDB space
http://jira.mongodb.org/browse/SERVER-142
Use:
db.users.find({'usertimes.title': "My Awesome Title"}, {'idle': 1});
May I suggest you take a more detailed look at http://www.mongodb.org/display/DOCS/Querying, it'll explain things for you.