How to change a field & add a sub-field in Mongodb - mongodb

I have mongo documents like the following:
{
"_id" : ObjectId("591e36eb2cdce09936d8e511"),
"bday" : null,
"studentId" : "000004"
}
I want to make it :
{
"_id" : ObjectId("591e36eb2cdce09936d8e511"),
"bday" : null,
"studentId" : {
"id" : "000004"
}
}
I have tried the following:
db.persons.update({"studentId": "000004"},{$set : {"studentId.id": "000004"}},false,true)
But it is giving an error:
LEFT_SUBFIELD only supports Object: studentId not: 2
Can anyone help?
Thanks,
Sumit.

In your case you see an error because you are trying to take "studentId" which is string and add a property inside it. To fix it you should set entire new object like this
db.persons.update({"studentId":"000004"},{$set:{"studentId":{"id":"000004"}}})
Another option, if, for example you need to change structure for entire collection, is to just iterate over each element and update them. Like this:
db.persons.find({}).forEach(function (item) {
if (item.studentId != null){
item.studentId = {id:item.studentId};
db.persons.save(item);
}
});

I hope this will help
db.collection.update({_id:ObjectId("591e36eb2cdce09936d8e511")},{$set:{"studentId":{"id":"000004"}}})

Related

MongoDB Compound text search

I have a collection like this in my mongo database, let's say it's called taxonomic.
{
"_id" : ObjectId("5810e15a762a39b41912a131"),
"validName" : "Eros",
"idUser" : ObjectId("1")
}
{
"_id" : ObjectId("5810e15a762a39b41912a132"),
"validName" : "Eros",
"idUser" : ObjectId("2")
}
I've already created a compound index to be able to search for the two values I want, such as this.
db.taxonomic.createIndex({"idUser":1,"validName":1})
Now, I want to be able to search and get a return from it only when both of the parameters are found on the same document of the collections, here's my try:
db.taxonomic.find({$text:{$search:"Eros 2"}},{idUser:1,validName:1})
The problem with this method is that it will return any match of "Eros" OR "2", what I want is a return of the values when "Eros" AND "2" are matched in a document of the collection.
Thank you for any help!
I dont think you require a text Index for it if you only want specific string
db.taxonomic.find({"$or" : [{"validName" : "Eros"},{"validName" : "2"}]},{idUser:1,validName:1})

Searching with dynamic field name in MongoDB

I have a situation where records in Mongo DB are like :
{
"_id" : "xxxx",
"_class" : "xxxx",
"orgId" : xxx,
"targetKeyToOrgIdMap" : {
"46784_56139542ecaa34c13ba9e314" : 46784,
"47530_562f1bc5fc1c1831d38d1900" : 47530,
"700004280_56c18369fc1cde1e2a017afc" : 700004280
},
}
I have to find out the records where child nodes of targetKeyToOrgIdMap has a particular set of values. That means, I know what the value is going to be there in the record in "46784_56139542ecaa34c13ba9e314" : 46784 part. And the field name is variable, its combination of the value and some random string.
In above example, I have 46784, and I need to find all the records which have 46784 in that respective field.
Is there any way I can fire some regex or something like that or by using any other mean where I would get the records which has the value I need in the child nodes of the field targetKeyToOrgIdMap.
Thanks in advance
You could use MongoDB's $where like this:
db.myCollection.find( { $where: function() {
for (var key in obj.targetKeyToOrgIdMap) {
if (obj.targetKeyToOrgIdMap[key] == 46784){
return true;
}
}
}}).each { obj ->
println obj
}
But be aware that this will require a full table scan where the function is executed for each document. See documentation.

Querying mongodb

I have a collection of emails in MongoDB with a field containing an array of json.
How can knowing the sender and the receiver, how can I find all the emails exchanged between the two people ? I need to do something like
db.email.find({"from": i.st20#gmail.com", "tos":"ron#gmail.com")
but I cant find the right way to write this query :(
> db.emails.findOne()
{
"from" : {
"real_name" : "it",
"address" : "i.st20#gmail.com"
},
"tos" : [
{
"real_name" : null,
"address" : "ron#gmail.com"
}
],
}
Use "from.address" and "tos.address":
db.emails.find({"from.address" : "i.st20#gmail.com", "tos.address" : "ron#gmail.com"})
Each field is considered as a json, we can precise the expected value through a "." :
db.emails.find({"tos.address" : "ron#gmail.com", "from.address":"i.st20#gmail.com"})
From and To are objects and data inside them can be accessed through a . operator.
So the query will be:
db.emails.find({"from.address" : "i.st20#gmail.com", "tos.address" : "ron#gmail.com"}).

Pig & MongoDB - How to load document nested fields with MongoLoader?

I have a set of document stored on mongodb, which are like this
{
"_id" : { "$oid" : "5201ca52ddf19f9c7aea0bb2"} ,
"id" : 1 ,
"path" : "C://..." ,
"experiences" : [
{ id = "1", date="12/2012", content="blabla" }
{ id = "2", date="12/2013", content="blabla2" }
]
}
I would like to process the "experiences" fields of these documents to obtain an output like this:
(1,1,12/2012,blabla)
(1,2,12/2013,blabla2)
the schema is (document_id,exp_id,exp_date,exp_content).
I'm loading the document via pig and MongoLoader, here is my code:
REGISTER /root/mongo-2.10.1.jar
REGISTER /root/pig_librairies/mongo-hadoop_cdh4.3.0-1.1.0.jar
REGISTER /root/pig_librairies/mongo-hadoop-pig_cdh4.3.0-1.1.0.jar
REGISTER /root/pig_librairies/mongo-hadoop-core_cdh4.3.0-1.1.0.jar
persons = LOAD 'mongodb://localhost/gestion_competences.cv'
USING com.mongodb.hadoop.pig.MongoLoader('id:chararray, path:chararray, experiences:charrarray)
AS (id, path, experiences);
I know the problem is here:
experiences:chararray
but I don't know what structure i could use. I tried bags and maps and it doesn't work...
Do you have an idea on how to solve the problem ?
Thanks
Try experiences:map[] and then you can access values via key as experiences#'content'

Ordering fields from find query with projection

I have a Mongo find query that works well to extract specific fields from a large document like...
db.profiles.find(
{ "profile.ModelID" : 'LZ241M4' },
{
_id : 0,
"profile.ModelID" : 1,
"profile.AVersion" : 2,
"profile.SVersion" : 3
}
);
...this produces the following output. Note how the SVersion comes before the AVersion in the document even though my projection asked for AVersion before SVersion.
{ "profile" : { "ModelID" : "LZ241M4", "SVersion" : "3.5", "AVersion" : "4.0.3" } }
{ "profile" : { "ModelID" : "LZ241M4", "SVersion" : "4.0", "AVersion" : "4.0.3" } }
...the problem is that I want the output to be...
{ "profile" : { "ModelID" : "LZ241M4", "AVersion" : "4.0.3", "SVersion" : "3.5" } }
{ "profile" : { "ModelID" : "LZ241M4", "AVersion" : "4.0.3", "SVersion" : "4.0" } }
What do I have to do get the Mongo JavaScript shell to present the results of my query in the field order that I specify?
I have achieved it by projecting the fields using aliases, instead of including and excluding by 0 and 1s.
Try this:
{
_id : 0,
"profile.ModelID" :"$profile.ModelID",
"profile.AVersion":"$profile.AVersion",
"profile.SVersion":"$profile.SVersion"
}
I get it now. You want to return results ordered by "fields" rather the value of a fields.
Simple answer is that you can't do this. Maybe its possible with the new aggregation framework. But this seems overkill just to order fields.
The second object in a find query is for including or excluding returned fields not for ordering them.
{
_id : 0, // 0 means exclude this field from results
"profile.ModelID" : 1, // 1 means include this field in the results
"profile.AVersion" :2, // 2 means nothing
"profile.SVersion" :3, // 3 means nothing
}
Last point, you shouldn't need to do this, who cares what order the fields come-back in.
You application should be able to make use of the fields it needs regardless of the order the fields are in.
Another solution I applied to achieve this is the following:
db.profiles
.find({ "profile.ModelID" : 'LZ241M4' })
.toArray()
.map(doc => ({
profile: {
ModelID: doc.profile.ModelID,
AVersion: doc.profile.AVersion,
SVersion: doc.profile.SVersion
}
}))
Since version 2.6 (that came out in 2014) MongoDB preserves the order of the document fields following the write operation (source).
P.S. If you are using Python you might find this interesting.