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" }}})
Related
I am trying to group multiple documents by addresses present in each document. However, the addresses are sub-documents themselves, and stored in an array. I require to group two documents together if they have even one same address present in their arrays, but not necessarily in the same index. Could this be done? The general structure of a document is as follows:
{
"_id" : ObjectId("5ccb0983258f7a1694f30e7d"),
"name" : {
"first" : "John",
"middle" : "J",
"last" : "Doe",
"prefix" : "Mr.",
"professionalSuffixes" : [
"MD"
],
"generationalSuffix" : "Jr"
},
"ssn" : "123-45-6789",
"birthDate" : "1996-06-28",
"gender" : "Unknown",
"maritalStatus" : "Separated",
"postalAddresses" : [
{
"streetAddress" : [
"23 LeeWay RD",
"APT 342"
],
"officeSuite" : "4743",
"apartmentNumber" : "022",
"postOfficeBoxNumber" : "12345",
"postalCode" : "12345",
"city" : "St Louis",
"state" : {
"code" : "MO",
"name" : "Missouri",
"description" : "Test Description"
},
"country" : {
"name" : "United States of America",
"iso2Code" : "US",
"iso3Code" : "USA",
"description" : "Test Description"
}
}
]
}
Thanks in advance.
Based on your comment responding to mine you just need to unwind the field first:
{
$unwind: "$postalAddresses"
},
{
$group: {
_id: group_cond,
docs_ids: {$push: "$_id"}
}
}
Now group condition should be whatever makes an address "unique",
it should look a little something like this:
{ country: "$postalAddresses.country.name", state: "$postalAddresses.state.code", city: "$postalAddresses.city", street: "$postalAddresses.street."}
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);});
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/
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?
I would like to only return my embedded documents titled 'Listings', based on the text search query. Where may I be going wrong? Creation of the index?
Here is my index:
db.Collection.ensureIndex({"Listings.Title": "text", "Listings.Description" : "text"}, {name: "Search"})
This returns the whole object, only want listings
db.AspNetUsers.runCommand("text", { search: "lawn" })
this returns just listings, but all the listings are included. Not the listings based on the search criteria e.g. 'lawn'
db.AspNetUsers.runCommand("text", { search: "lawn", project: {Listings:1}})
here is my object
{
"_id" : ,
"UserName" : "",
"PasswordHash" : "",
"SecurityStamp" : "",
"Roles" : [],
"Claims" : [],
"Logins" : [],
"ProfileData" : {
"BirthDate" : new Date("3/8/1974 00:00:00"),
"FirstName" : "",
"LastName" : "",
"MiddleName" : "",
"Address" : "",
"Address1" : ,
"City" : "",
"State" : "",
"PostalCode" : "",
"CellPhone" : "",
"HomePhone" : "",
"Location" : {
"type" : "Point",
"coordinates" : [, ]
}
},
"Email" : "",
"ConfirmationToken" : "Confirmed",
"IsConfirmed" : true,
"Listings" : [{
"_id" : ObjectId("5331ac28a5eabf2854085df5"),
"UserId" : ObjectId("5329b43fa5eabf0548490c27"),
"Title" : "Lawn Chairs",
"Description" : "lawn chairs",
"Pictures" : ["5331ac28a5eabf2854085df6", "5331ac28a5eabf2854085df7", "5331ac28a5eabf2854085df8"],
"Category" : {
"_id" : ObjectId("53273ce37dd6c71e1859ab77"),
"Title" : "Leisure"
}
}, {
"_id" : ObjectId("5331ac50a5eabf2854085df9"),
"UserId" : ObjectId("5329b43fa5eabf0548490c27"),
"Title" : "Lawn Ornaments",
"Description" : "lawn ornaments troll frog gnome",
"Pictures" : ["5331ac50a5eabf2854085dfa", "5331ac50a5eabf2854085dfb", "5331ac51a5eabf2854085dfc"],
"Category" : {
"_id" : ObjectId("53273cd57dd6c71e1859ab76"),
"Title" : "Home"
}
}, {
"_id" : ObjectId("5331ac71a5eabf2854085dfd"),
"UserId" : ObjectId("5329b43fa5eabf0548490c27"),
"Title" : "Cell Phone",
"Description" : "Samsung Galaxy S4",
"Pictures" : ["5331ac71a5eabf2854085dfe", "5331ac71a5eabf2854085dff", "5331ac72a5eabf2854085e00"],
"Category" : {
"_id" : ObjectId("53273cd57dd6c71e1859ab76"),
"Title" : "Home"
}
}]
}
You can get just the fields you want using a project parameter. For getting just the Listings element, you could try:
db.AspNetUsers.runCommand("text", {search:"lawn", project:{_id:0, Listings:1}})
EDIT:
The text command will return all documents that contain the search term. You'll get the entire document. If you further want to filter an array inside of the document, you can use the $elemMatch projection operator, in combination with $regex. For example:
db.AspNetUsers.runCommand("text", {
search: "lawn",
project: {_id:0, Listings:{$elemMatch:{ "Title": /lawn/i }}}
})