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

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/

Related

Facets and lookup in Mongodb C#

I have 2 facet on User collection, which are concatenated to create a new array. For each document, i have reference ids to address collection. I need to get the detail about the address. Can it be done on single query using lookup on existing facets together.
Any help is appreciated.
User collection: [
{ "id" : "123456", "name" : "foo", "addressIds" :
[ObjectId(234567)] }, "id" : "345678", "name" : "bar",
"addressIds" : [ObjectId(678565), ObjectId(567456)]
}]
Address collection: [
{ "_id":"234567", "district" : "district1", "pincode" : "568923", },
{ "_id":"678565", "district" : "district2",
"pincode" : "568924", }, { "_id":"567456", "district" :
"district3", "pincode" : "568925", }]

MongoDB : Push data to a map

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);});

Mongodb : How to select objects where an array contains only a specific field?

I have two objects :
{
"_id" : ObjectId("54be5f5528c13bfc3409e8c2"),
"name" : "Antonio",
"lastname" : "de Cabezón",
"by" : 1510,
"dy" : 1566,
"country" : "spain",
"genre" : [
"classical",
"baroque"
]
}
{
"_id" : ObjectId("54be5f5528c13bfc3409e8c1"),
"name" : "Guillaume-Antoine",
"lastname" : "Calvière",
"by" : 1695,
"dy" : 1755,
"country" : "france",
"genre" : [
"baroque"
]
}
When i do a db.currentdb.find({genre: 'baroque'}), it returns me the first object too.
I'd like to fetch only the object where the genre is only "baroque". What would be the proper way to do it ?
You could try
db.currentdb.find({genre: ['baroque']})
Also, take a look to the documentation:
https://docs.mongodb.org/manual/reference/method/db.collection.find/

mongodb: how to add a new property for mongo's geolocation function

I have a collection of events in mongodb, and below is on sample of the document in the collection:
{
"id" : 15178390976,
"title" : "Basics of Writing a Business Plan (Essex Office)",
"end_date" : "2015-03-18 11:00:00",
"venue" :
{
"city" : "Essex",
"name" : "WindsorEssex Small Business Centre (Essex Office)",
"country" : "Canada",
"region" : "Ontario",
"longitude" : -82.823545,
"postal_code" : "N8M 2J3",
"address" : "39 Maidstone Avenue East",
"latitude" : 42.17792,
"id" : 8864285,
"Lat-Long" : "42.17792 / -82.823545"
}
}
I want to modify the collection of documents, so that I can apply the geospatial spherical function in MongoDB. I want to modify all the documents in the collection as follows:
{
"id" : 15178390976,
"title" : "Basics of Writing a Business Plan (Essex Office)",
"end_date" : "2015-03-18 11:00:00",
"venue" :
{
"city" : "Essex",
"name" : "WindsorEssex Small Business Centre (Essex Office)",
"country" : "Canada",
"region" : "Ontario",
"longitude" : -82.823545,
"postal_code" : "N8M 2J3",
"address" : "39 Maidstone Avenue East",
"latitude" : 42.17792,
"id" : 8864285,
"Lat-Long" : "42.17792 / -82.823545"
},
location:
{
"type":"Point",
"coordinates":[-82.823545, 42.17792]
}
}
the value in the coordinates are from venue.longitude and venue.latitude.
How can I update the documents to what I want? I have tried $update and $projection in aggregation, none of them are working for the location.coordinates property. I failed to push the values into the array.
the aggregation I use is:
db.coll.aggregation([
{ $project:
{
"id": "$id",
"title":"$title",
"venue":"$venue",
"location.type": "Point",
"location.coordinate":["$venue.longitude","$venue.latitude"]
}
},
{ $out:newoutput }
])
Anyone can please help me?

How do I $pull array element by field value (when elements are objects)

I am using MongoDB and trying to remove array elements (themselves embedded documents) from documents in a DB matching a criteria. FOr this I am trying to use the $pull operator in the update command. But I am unable to make this work in some cases (See description below). What am I missing?
Thanks in advance.
-Sachin
> use test
switched to db test
//First, insert a record with an array of addresses, with array elements being embedded objects with exactly 1 element (email)
> db.users.insert({
name: 'smith',
addresses:[{email:'a#b'},{email:'c#d'}]
});... ... ...
//Result of the insertion
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "a#b" }, { "email" : "c#d" } ] }
//From records with name= Smith, try to $pull any array elements with email a#b
> db.users.update({name:'smith'}, {$pull:{addresses:{email:'a#b'}}});>
//After successful $pull
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c#d" } ] }
//Now insert a record with an array of addresses, with array elements being embedded objects with exactly 2 elements (email, phone)
> db.users.insert({
name: 'smith',
addresses:[{email:'a#b', phone: '12345'},{email:'c#d',phone :'54321'}]
});... ... ...
//Result of the insertion
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c#d" } ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
{
"email" : "a#b",
"phone" : "12345"
},
{
"email" : "c#d",
"phone" : "54321"
}
] }
//From records with name= Smith, again try to $pull any array elements with email a#b
> db.users.update({name:'smith'}, {$pull:{addresses:{email:'a#b'}}})
// - Unsuccessful $pull (Why? How to fix this)
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c#d" } ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
{
"email" : "a#b",
"phone" : "12345"
},
{
"email" : "c#d",
"phone" : "54321"
}
] }
//Meanwhile, the single element pull still works as before -
> db.users.update({name:'smith'}, {$pull:{addresses:{email:'c#d'}}})
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
{
"email" : "a#b",
"phone" : "12345"
},
{
"email" : "c#d",
"phone" : "54321"
}
] }
>
Thanks for the resposen, although that didn't work. Here is the transcript of the Mongo shell.
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
{
"email" : "a#b",
"phone" : "12345"
},
{
"email" : "c#d",
"phone" : "54321"
}
] }
> db.users.update({name:'smith'}, {$pull:{"addresses.email": 'a#b'}})
Modifier spec implies existence of an encapsulating object with a name that already represents a non-object, or is referenced in another $set clause
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
{
"email" : "a#b",
"phone" : "12345"
},
{
"email" : "c#d",
"phone" : "54321"
}
] }
>
...so basically the dot notation didnt work out.
Ok, so I found the answer.
First thing, I was using MongoDB 1.2.2, and this version didnt support the update $pull operation as described above.
Next, I upgraded to MongoDB 2.06 (latest stable). Then when I use the old database created in 1.2.2, the same result.
Next, I created a new DB in 2.06 and then tried the suggestion by #sergio-tulentsev, i.e.
db.users.update({name:'smith'}, {$pull:{"addresses.email": 'a#b'}})
Unfortunately, this didnt work either.
Last, I tried the initial command I had not been able to execute
db.users.update({name:'smith'}, {$pull:{addresses:{email:'a#b'}}})
And it worked!!!
So takeaway:
1. Update MongoDB server
2. Old version of Database file wont work, only works with new database file. Now I need to somehow migrate my data to the newer version.
UPDATE:...this migration was as simple as issuing the mongod --upgrade command.