Mongodb field value as another field - mongodb

I just want to get filed value where field is value of another filed.
ex: below I want to get valueOf("Brand")="Toshiba" where "Brand" is value of field mappingData.brand
{
"_id" : ObjectId("5640bdec1b988de0be317251"),
"Part number" : "PS460E-04NVX-NL",
"Brand" : "Toshiba",
"Quality" : "XYZ",
"Category" : "notebooks",
"Model Name" : "Satellite Pro 4600 PIII900",
"EAN" : "",
"Market Presence" : "N",
"Family" : "",
"Title" : "Toshiba Satellite Pro 4600 PIII900",
"mapping" : {
"brand" : "Brand",
"title" : "Title"
}
}
I am trying this following way but it is returning undefined.
db.products.find().limit( 5 ).forEach(function(myDoc) {
var b=myDoc.mappingData.brand;
print(myDoc.$$b);
})
Please help to resolve this issue.

Use the bracket notation to access the property of an object. In your case, this would be
db.products.find().limit(5).forEach(function(myDoc) {
var brand = myDoc.mappingData.brand,
brandVal = myDoc[brand];
print(brandVal);
})

Related

Update multiple data in single array element using object id using mongodb

I am Very Beginner in mongodb.I have mongodb data with multiple array elements just update multiple data in single array element
"_id" : ObjectId("6217138f0607ea2e07e70b25"),
"phone_number" : "7645645676",
"email" : "test02#gmail.com",
"name" : "John",
"address" : "44th street Englan",
"devices" : [
{
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
"_id" : ObjectId("6217138f0607ea2e07e70b26"),
},
{
"ime_number" : "HHH555",
"device_name" : "Harry Potter",
"subscription_type" : "Monthly",
"validity" : 32,
"_id" : ObjectId("621714570607ea2e07e70b2b"),
}
]
Only This data
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
Changed data
"ime_number" : "888844442222",
"device_name" : "Remote Device",
"subscription_type" : "Monthly",
"validity" : 30,
Help me to solve this
I think you can use $ operator to do this.
The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.
Now you have two options, to either update the individual fields (only the ones you want to) or update the whole object. For both of them you need $set :
Just pass the query as first argument of your update query and the second argument is your update clause.
Collection.update({'devices.device_name' : 'Test456356'}, {
{ '$set': { 'devices.$': {
"ime_number" : "888844442222",
"device_name" : "Remote Device",
"subscription_type" : "Monthly",
"validity" : 30,
} } }
});
Note: above updates all the elements found with the query. If you want to be specific you can use updateOne. Just replace update with updateOne and it will update the first matched document

Mongodb copy field to another array field type [duplicate]

This question already has answers here:
Update MongoDB field using value of another field
(12 answers)
How do I perform the SQL Join equivalent in MongoDB?
(19 answers)
Closed 3 years ago.
I want to create a migration script that able to copy from Project assignedTo field to Project assignedMultiple but inserted in an array with the users detail on it and assignedTo field will be null. assignedTo field will be deprecated since we were gonna use assignedMultiple.
This is my users table:
User:
{
"username" : "usertest",
"firstName" : "Stack",
"lastName" : "Overflow",
"company" : "",
"country" : "",
"id" : ObjectId("this_is_usertest_id")
}
Project:
{
"name" : "Project",
"assignedTo" : ObjectId("this_is_usertest_id"),
"assignedMultiple" : [],
"id" : ObjectId("someid")
}
Expected Output:
{
"name" : "Project",
"assignedTo" : null,
"assignedMultiple" : [{
"username" : "usertest",
"firstName" : "Stack",
"lastName" : "Overflow",
"company" : "",
"country" : "",
"id": ObjectId("this_is_usertest_id")
}]
"id" : ObjectId("someid")
}
This is what I have tried so far but nothing happens and giving me an error: failed to execute a script. TypeError: db.user.findOne(...) is null but when I try to use print(project) it prints the projects with data.
This one is based on this link MongoDB : how to set a new field equal to the value of another field, for every document in a collection
db.project
.find().forEach(function(project)
{
if (project.assignedTo) {
db.user.findOne({ where: { id: project.assignedTo } })
.forEach(function(user) {
var userObj = {
"username" : user.username,
"firstName" : user.firstName,
"lastName" : user.lastName,
"company" : user.company,
"country" : user.country,
"id" : user.id
};
db.getCollection('project').update(
{"_id" : project.id}, {$addToSet : {assignedMultiple : [...project.assignedMultiple, { "_id": project.assignedTo }]}}
)
});
}
});
Thank you in advance!

Best way to create index for MongoDB

I am having records stored in mongo-db collection for customer and there transactions with below format:
{
"_id" : ObjectId("59b6992a0b54c9c4a5434088"),
"Results" : {
"id" : "2139623696",
"member_joined_date" : ISODate("2010-07-07T00:00:00.000+0000"),
"account_activation_date" : ISODate("2010-07-07T00:00:00.000+0000"),
"family_name" : "XYZ",
"given_name" : "KOKI HOI",
"gender" : "Female",
"dob" : ISODate("1967-07-20T00:00:00.000+0000"),
"preflanguage" : "en-GB",
"title" : "MR",
"contact_no" : "60193551626",
"email" : "abc123#xmail.com",
"street1" : "address line 1",
"street2" : "address line 2",
"street3" : "address line 3",
"zipcd" : "123456",
"city" : "xyz",
"countrycd" : "Malaysia",
"Transaction" : [
{
"txncd" : "411",
"txndate" : ISODate("2017-08-02 00:00:00.000000"),
"prcs_date" : ISODate("2017-08-02 00:00:00.000000"),
"txn_descp" : "Some MALL : SHOP & FLY FREE",
"merchant_id" : "6587867dsfd",
"orig_pts" : "0.00000",
"text" : "Some text"
}
]
}
I want to create index on fields "txn_descp", "txndate", "member_joined_date", "gender", "dob" for faster access. Can some one help me in creating index for this document? Will appreciate any kind of help and suggestions.
While creating the index there are a few things to keep in mind.
Always create the index for the queries you use.
Go for compound indexes whenever possible.
First field in the index should be the one with the minimum possible values.Ie, if there is an index with gender and DOB as keys, It is better to have {gender:1,dob:1}

Getting data range from firebase with swift

I would like to ask that question. The question is how to get specific data range from firebase ?
I have table on firebase like this:
"users" : {
"Jz3IpatRWiWoDbiYM62q6qbHB503" : {
"email" : "Kaanozdemir#gmail.com",
"lastName" : "Ozdemir",
"name" : "Kaan"
},
"PmeYYFiac0c55fU2sFpnTP308mC3" : {
"email" : "kevinhart#gmail.com",
"lastName" : "Hart",
"name" : "Kevin"
},
"r0bMqSGCWihFi2EF4u6ckSzLP8v1" : {
"email" : "Marcusalvarez#gmail.com",
"lastName" : "Alvarez",
"name" : "Marcus"
},
"A3tmSSGCWihFi2EF4u6ckSzLP8c1" : {
"email" : "taylorswift#gmail.com",
"lastName" : "Swift",
"name" : "Taylor"
},
"3SUTsiGCWihFi2EF4u6ckSzLP8v2" : {
"email" : "jimmyfellon#gmail.com",
"lastName" : "Fellon",
"name" : "Jimmy"
},
"lgSit3GCWihFi2EF4u6ckSzLP8u3" : {
"email" : "jaxteller#gmail.com",
"lastName" : "Teller",
"name" : "Jax"
}
For example, I would like to get users values between 2 and 4 [2 - 4](Marcus Alvarez - Taylor Swift - Jimmy Fellon).
Is there any way to do that server side ? I don't wanna get all data and pick values that I want. Anyone knows?
Change your JSON DB structure to include an index in every node :
"users" : {
"autoID1" : {
"email" :.....,
"lastName" : ......,
"name" :.......,
"index" : //e.g.. 1,2,3,4......
}
},
"noOfUsers" : 223,
If you are appending this users node via app, you have too keep track of the no of users in Database node users and keep updating the noOfUsers whenever a new user is added. And to set the next ones index number , just retrieve that node value i.e 223 and sees it and then increment the noOfUsers......
To retrieve between 2-4 .. Now you can use :
Database.database().reference().child("users").queryOrdered(byChild: "index").queryStarting(atValue: "2").queryEnding(atValue: "4").observe....

Searching in json document using robomongo

I have a json document as follows:
{
"_id" : ObjectId("553547d3864c23ae8837f8f2"),
"labels" : {
"id" : "1",
"name" : "Planet E",
"sublabels" : "Antidote (4)Community ProjectsGuilty PleasuresI Ner Zon SoundsPlanet E Communications, Inc.TWPENTY",
"contactinfo" : "Planet E Communications\nP.O. Box 27218\nDetroit, 48227, USA\n\np: 313.874.8729 \nf: 313.874.8732\n\nemail: info AT Planet-e DOT net\n",
"profile" : "Classic Techno label from Detroit, USA.\n[b]Label owner:[/b] [a=Carl Craig].\n",
"dataquality" : "Needs Vote",
"urls" : "http://planet-e.nethttp://planetecommunications.bandcamp.comhttp://www.discogs.com/user/planetedetroithttp://www.facebook.com/planetedetroithttp://www.flickr.com/photos/planetedetroithttp://plus.google.com/100841702106447505236http://myspace.com/planetecomhttp://myspace.com/planetedetroithttp://soundcloud.com/planetedetroithttp://twitter.com/planetedetroithttp://vimeo.com/user1265384http://www.youtube.com/user/planetedetroithttp://en.wikipedia.org/wiki/Planet_E_Communications"
}
}
When I use the following command
db.labels.find({name: "Planet E"})
I get the following result
What am I doing wrong?
You need to search for db.labels.find({ "labels.name": "Planet E" }). The name property is under the labels key.