MongoDB - Need help updating an Object within an Array of a Document - arrays

Situation:
I have a collection named 'employees'. This collection contains an array of managers. Each manager object has a property called 'directReports'. This property is an array of objects representing their subordinates. For example:
db.employees.find({ name: 'John Smith' });
{
"id" : 1234,
"name" : "John Smith",
"FirstName" : "John",
"LastName" : "Smith",
"SamAccountName" : "jsmith",
"DepartmentName" : "Management",
"SupID" : 0,
"Email" : "jsmith#company.tld",
"SupSamAccountName" : "bross",
"SupName" : "Bob Ross",
"directReports" : [
{
"id" : 5678,
"name" : "Jane Doe",
"FirstName" : "Jane",
"LastName" : "Doe",
"SamAccountName" : "jdoe",
"DepartmentName" : "Agent",
"SupID" : 1234,
"Email" : "jdoe#company.tld",
"SupSamAccountName" : "jsmith",
"SupName" : "John Smith",
},...]}
What I'd Like to Do:
I'd like to add a new property called 'status' (with a value of 'Lunch') to Jane Doe.
How can I do this?
Any help is extremely appreciated!

Assuming SamAccountNames are unique:
db.employees.update({
{ SamAccountName: "jsmith", directReports.SamAccountName: "jdoe" },
{ $set: { directReports.$.status: "Lunch" } }
})
Based on this question's tags, since you're using Mongoose, be sure to update your schema appropriately. You might consider reworking your schema to take advantage of Mongoose query population.

I think you need to add 'status' with the value of 'launch' to your model and after create a function to update all the employees data.
Hope it helps

Related

How can I remove duplicates from Firebase database when nodes share a common child?

"-Kj9Penv_LMRUIPSet0b" : {
"categories" : [ "food", "fashion"],
"contact" : "profile/contact/eieiiieie888x7ww28288_x22",
"location" : "New York, United States",
"name" : "Billybob Smith",
"social" : {
"twitter" : {
"followers" : "1,002",
"nickname" : "#billybob"
}
},
"state" : "0"
},
"eieiiieie888x7ww28288_x22" : {
"categories" : [ "food", "fashion" ],
"contact" : "profile/contact/eieiiieie888x7ww28288_x22",
"location" : "New York, United States",
"name" : "Billybob Smith.",
"social" : {
"twitter" : {
"followers" : "1,002",
"nickname" : "#billybob"
}
},
"socialID" : "twitter_id|558969977",
"state" : "0",
"uniqueID" : "eieiiieie888x7ww2828"
},
This is one .JSON example of a duplicate in my database. I have a lot of duplicates in my database. The only common piece of data I capture which uniquely identifies each user is their contact link. What is my best course of action to seek and remove duplicates from my database? I'm totally stuck. The second entry example is the more accurate and complete entry. Ideally, I could remove the first one and leave the second one behind.
Could really use some help here! Thank you so much!

Getting array length in mongodb?

{
"_id" : 654321,
"first_name" : "John",
"last_name" : "Doe",
"interested_by" : [ "electronics", "sports", "music" ],
"address" : {
"name" : "John Doe",
"company" : "Resultri",
"street" : "1015 Mapple Street",
"city" : "San Francisco",
"state" : "CA",
"zip_code" : 94105
}
}
How can i find the name of elements in array 'intersted_by' using command??
You can have the size of your result in the mongo shell using :
db.collection.count()
Replace collection by the name of your collection. You can also add a find condition like this :
db.collection.find().count()
Like that, you wan restrict your result with deffernts clauses before count the numbers of data corresponding
Edit : don't forget to do the command use databaseName
if you're not in your database, it does'nt work
You can count the amount of keys by doing:
var count = Object.keys(myObject).length;

Create a document with an array from filtered elements in an existing document array

I've asked this question before but not in the clearest way since I had no responses :( so I thought I would try again.
I have a document as shown below, I want to create a new document which only picks the names in the array where language = "English".
{
"_id" : ObjectId("564d35d5150699558156942b"),
"objectCategory" : "Food",
"objectType" : "Fruit",
"objectName" : [
{
"language" : "English",
"name" : "Apple"
},
{
"language" : "French",
"name" : "Pomme"
},
{
"language" : "English",
"name" : "Strawberry"
},
{
"language" : "French",
"name" : "Fraise"
}
]
}
I want the $out document to look like this below. I know I can filter a document by content but, I want to filter within a single document not across a collection. For getting the right document in the first place, I would have a query to $find objectCategory = "Food" and objectType = "Fruit"
{
"_id" : ObjectId("564d35d5150699558156942b"),
"objectCategory" : "Food",
"objectType" : "Fruit",
"objectName" : [
"name" : "Apple",
"name" : "Strawberry"
]
}
Thanks, Matt
wow, ah, I really thought I found it with:
db.serviceCatalogue.find({objectName: {"$elemMatch": {language: "English"}}}, {"objectName.name": 1})
;thanks to: Retrieve only the queried element in an object array in MongoDB collection
However, it did nothing, I must have dreamt it worked. How do you just get the array positions where the value of a field called language = 'English'?
this is only an example of what I want to do, it seems like this is just painful, especially with no-one answering other than me :)

postgresql json array index

I have data for customers with more than one adresses with json representation like this:
{
"firstName" : "Max",
"lastName" : "Mustermann",
"addresses" : [{
"city" : "München",
"houseNumber" : "1",
"postalCode" : "87654",
"street" : "Leopoldstraße",
}, {
"city" : "Berlin",
"houseNumber" : "2a",
"postalCode" : "12345",
"street" : "Kurfürstendamm",
}
]
}
these json is stored in a column named json of datatype json in a table named customer.
I want to query like this:
SELECT *
FROM customer cust,
json_array_elements(cust.json#>'{addresses}') as adr
WHERE adr->>'city' like '%erlin'
and adr->>'street' like '%urf%';
Query works fine ... but can't create index that postgresql 9.3.4 can use.
Any idea?

Mongodb: query using value from a nested array

I have the following schema, blog collection & friendscoll as below
blogpostcollection
{
"_id" : ObjectId("4fff0bf18bf0d19c4f1a5826"),
"author" : "joe",
"text" : "Here is the text...",
"userid" : 0
}
{
"_id" : ObjectId("4fff0bf18bf0d19c4f1a5827"),
"author" : "blake",
"text" : "Here is the text...",
"userid" : 1
}
{
"_id" : ObjectId("4fff0bf18bf0d19c4f1a5828"),
"author" : "joe",
"text" : "Here is the text...",
"userid" : 2
}
myfriendscoll
{
"myid": 999,
"data": [
{
"uid": 1,
"name": "Raul"
},
{
"uid": 3,
"name": "John K"
} ]
}
I want to find all documents in blogpostcollection, where the userid exists as uid, in the myfriendscoll collection.
So in effect, something like..
var user = db.myfriendscoll.findOne({"myid" : 999}, {"data.uid": 1});
db.blogpostcollection.find( {"userid" : {$in : user.data.uid}});
This doesn't work, but is there a way to get it to work?...Thanks!
If you are using development version 2.1 or when you move to 2.2 once it's released you can use the aggregation framework to get the format you want back from the first query:
var ret=db.myfriendscoll.aggregate([
{$match:{"myid" : 999}},
{$project:{_id:0,uid:"$data.uid"}}
]);
var uids=ret.result[0].uid;
db.blogpostcollection.find({userid:{$in:uids}})
You'll need to extract the actual uid values into an array to use with $in. Try this:
var user = db.myfriendscoll.findOne({"myid" : 999}, {"data.uid": 1});
var uids = user.data.map(function(v){ return v.uid });
db.blogpostcollection.find( {"userid" : {$in : uids}});

Resources