MongoDB : Push data to a map - mongodb

I have employee json as below in db, I want to create a map of "addressId"(key) and "city"(value) and return the result.
{
"_id" :1,
"_class" : "com.entity.Employee",
"clientId" : 1,
"addresses" : [
{
"addressId" : 1,
"street" : "ghi",
"city" : "Hyderabad"
},
{
"addressId" : 2,
"street" : "abc",
"city" : "Bangalore"
},
{
"addressId" : 3,
"street" : "def",
"city" : "Chennai"
}
]
}
Please suggest me which operator can I use and whether this can be achieved using $project.

Yes you have to use projection with unwind and forEach to make key value pair
try below mongo query
db.collection_name.aggregate([{"$unwind":"$addresses"},{"$project": {"addressId": "$addresses.addressId", "city":"$addresses.city", "_class":"$_class","clientId":"$clientId"} }]).forEach(function(ojb){ojb[ojb.addressId]=ojb.city; printjson(ojb);});

Related

How to fliter based on the embeded collection field in mongo db

I have a collection like the one below
{
"_id" : ObjectId("573eb77bf3465096ddb1e873"),
"id" : 1,
"name" : "siva",
"email" : "sivateja#gmail.com",
"address" : [
{ "street" : "balajinager", "district" : "nellore", "state" : "A.p" },
[ { "street" : "balajinager", "district" : "nellore", "state" : "A.p" } ]
]
}
I want to filter the records based on the city, which is inside the address array, how can we do that in mongo db?
district also fine?
{address:{$elemMatch:{district:"nellore"}}}
see https://docs.mongodb.com/manual/reference/operator/query/elemMatch/

How can I remove an embedded array in MongoDB?

I have a collection like:
{
"_id" : "oBmLTMB5Y6gWwkYML",
"mailingAddresses" : [
{
"addressId" : "sEK4cza8XBHBApF2P",
"streetAddress" : "asdf",
"streetAddress2" : "fdsa",
"city" : "asdfasdf",
"state" : "DC",
"zip" : "2201512"
},
{
"addressId" : "behnKE3THzcS9sH5E",
"streetAddress" : "ffff",
"streetAddress2" : "ddd",
"city" : "asdfsdf",
"state" : "CA",
"zip" : "99995"
}
]
}
How can I remove an entire address object? For example, I want to remove the address with id sEK4cza8XBHBApF2P. I have tried:
db.users.update({_id: 'oBmLTMB5Y6gWwkYML'}, {$pull: {'mailingAddress': {addressId: 'sEK4cza8XBHBApF2P'}}})
But it only returns 'nMatched: 1' and does not remove that address object.
I found the answer:
$pull can be used to remove embedded array:
db.users.update({_id: 'oBmLTMB5Y6gWwkYML'}, {$pull: {mailingAddresses: {addressId: "sEK4cza8XBHBApF2P" }}})

How to count all occurrences of an element in MongoDB?

Let's say I have the following entries in my MongoDB.
{ "_id" : ObjectId("5474af69d4b28042fb63b81b"), "name" : "a", "time" : NumberLong("1412774562000"), "location" : "DE" }
{ "_id" : ObjectId("5474af69d4b28042fb63b81c"), "name" : "b", "time" : NumberLong("1412774562020"), "location" : "DE" }
{ "_id" : ObjectId("5474af69d4b28042fb63b81d"), "name" : "c", "time" : NumberLong("1412774562040"), "location" : "US" }
{ "_id" : ObjectId("5474af69d4b28042fb63b81e"), "name" : "d", "time" : NumberLong("1412774562060"), "location" : "AU" }
{ "_id" : ObjectId("5474af69d4b28042fb63b81f"), "name" : "e", "time" : NumberLong("1412774562080"), "location" : "CN" }
As a result, I need to know how often each specific "location" can be found in the database e.g.
{"DE": "2",
"US": "1",
"AU": "1",
"CN": "1"}
I have no information about all the different locations in the database so querying after a known location for example
db.c.find({"location": "DE"})
would not solve my problem.
You need to make use of the aggregation pipeline with a group and project stage operators.
db.c.aggregate([
{$group:{"_id":"$location","count":{$sum:1}}},
{$project:{"location":"$_id","occurance":"$count"}}
])

Using aggregation in MongoDB to select two or more fields of a document

I'm starting to work with MongoDB and I've a question about aggregation. I've a document that use a lot of different fields in different orders. For example:
db.my_collection.insert({ "answers" : [ { "id" : "0", "type" : "text", "value" : "A"}, { "id" : "1", "type" : "text", "value" : "B"}]})
db.my_collection.insert({ "answers" : [ { "id" : "0", "type" : "text", "value" : "C"}, { "id" : "1", "type" : "text", "value" : "A"}]})
I would to execute a query using "answers.id" with "answers.value" to obtain a result.
I tried but didn't get results, in my case, I executed the command:
db.my_collection.aggregate({$match: {"answers.id":"0", "answers.value": "A"}})
And the result was the two responses when I expected only:
{ "answers" : [ { "id" : "0", "type" : "text", "value" : "A"}, { "id" : "1", "type" : "text", "value" : "B"}]
Thank you!!!
You need to use the $elemMatch operator to match a single element of the answers array with both the specified 'id' and 'value'.
Something like this should work:
db.my_collection.aggregate( {
"$match" : {
"answers" {
"$elemMatch" : {
"id" : "0",
"value" : "A"
}
}
}
} )

Deleting a single object from an array of objects in MongoDB

Say we have the following collection of documents:
{ "_id" : ObjectId("50a69fa904c8310609600be3"), "id" : 100, "city" : "San Francisco", "friends" : [ { "id" : 1, "name" : "John" }, { "id" : 2, "name" : "Betty" }, { "id" : 3, "name" : "Harry" } ] }
{ "_id" : ObjectId("50a69fc104c8310609600be4"), "id" : 200, "city" : "Palo Alto", "friends" : [ { "id" : 1, "name" : "Carol" }, { "id" : 2, "name" : "Frank" }, { "id" : 3, "name" : "Norman" } ] }
{ "_id" : ObjectId("50a69fc304c8310609600be5"), "id" : 300, "city" : "Los Angeles", "friends" : [ { "id" : 1, "name" : "Fred" }, { "id" : 2, "name" : "Neal" }, { "id" : 3, "name" : "David" } ] }
.
.
.
Now let's say that Frank (Palo Alto, id=2) is no longer my friend, and I want to delete him from the collection. I thought the following might work, but it doesn't:
db.test.update({"city":"Palo Alto"},{"$pull":{"friends.name":"Frank"}})
I'd like to be able to do something like that. Delete an object within an array within a collection of documents. How do you do this?
You were close. The query should be like this:
db.test.update({"city":"Palo Alto"},{"$pull":{"friends":{"name":"Frank"}}});
$pull takes an object whose field specifies the field array "friends". The value {"name":"Frank"} represents the query (to run inside the array) to find the element to pull out.