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"}).
Related
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"}}})
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})
Hello i'm new to Mongodb and i have a question that i haven't found an answer for it yet.
I would like to know how to find all the users that are younger than a certain age. db.getCollection('data').find({age:{$lt:50}}) is not working
I would like to know how to Extract all the mails of the users to a csv file.
Regards,
Nati
//'data'- is the a document/table
//The data looks like that :
db.getCollection('data').find({}) :
/* 1 */
{
"_id" : "8f911",
"userDetails" : { "age" : "19",
"birthday" : "1996/5/11"
},
"username" : "emailemail#do.com"
}
/* 2 */
.
.
.
.
age is nested inside userDetails. Can you try:
db.getCollection('data').find({"userDetails.age":{$lt:50}})
Since it is a string you can use JavaScript Expression for query. It will do the typecasting:
db.getCollection('data').find("this.userDetails.age < 50"}})
Gurus - I'm stuck in a situation that I can't figure out how I can query from the following collection "spouse", which has embedded document "surname" and check for equality with "surname" of this document:
{
"_id" : ObjectId("50bd2bb4fcfc6066b7ef090d"),
"name" : "Gwendolyn",
"surname" : "Davis",
"birthyear" : 1978,
"spouse" : {
"name" : "Dennis",
"surname" : "Evans",
"birthyear" : 1969
},
I need to query:
Output data for all spouses with the same surnames (if the surname of
one of the spouses is not specified, assume that it coincides with the
name of another)
I tried something like this:
db.task.find( {"surname" : { "spouse.surname" : 1 }} )
but it failed)
PLEASE PLEASE Guide me how I can achieve this any example/sample? based on this will be really helpful :-)
Thanks a lot!
You have three options.
Use $where modifier:
db.task.find({$where: 'this.spouse.surname === this.surname'})
Update all your documents and add special flag. After that you will be able to query documents by this flag. It's faster then $where, but requires altering your data.
Use MapReduce. It's quite complicated, but it allows you to do nearly anything.
While trying to setup to use the Geospatial Index on MongoDB, I run into the error message that the location array is not in the correct format.
This is my collection "test".
{
"_id" : ObjectId("4f037ac176d6fdab5b00000a"),
"CorporateId" : "XYZ12345",
"Places" : [
{
"Location" : {
"Longitude" : "50.0",
"Latitude" : "50.0"
},
"ValidFrom" : "2011-11-01 00:00:00",
"ValidTo" : "2021-12-31 00:00:00",
"itemCount" : "1"
}
]
}
Once I run this code.
db.test.ensureIndex({"Places.Location": "2d"});
I get this error message
location object expected, location array not in correct format
My assumption is that the Long/Lat needs to be a number.
Currently it is an object.
typeof db.test.Places.Location.Longitude -> Object
typeof db.test.Places.Location -> Object
My problem is that since I am still quite new to MongoDB, I don't really know how to approach this problem in the best way.
Mongodb expects numbers for the coordinates while you passed in a string.
"Location" : {
"Longitude" : 50.0, // <<<<<<<<<<<<<< use numbers instead
"Latitude" : 50.0
},
see http://www.mongodb.org/display/DOCS/Geospatial+Indexing for details.
The problem has been fixed by converting the Location parameters into a float type like this.
$var = $JSonString['Places'];
$test=count($var);
$i=0;
for ( $i=0; $i<$test;$i++){
$lon = (float)$JSonString['Places'][$i]['Location']['Longitude'];
$lat = (float)$JSonString['Places'][$i]['Location']['Latitude'];
$JSonString['Places'][$i]['Location']['Longitude'] =$lon ;
$JSonString['Places'][$i]['Location']['Latitude'] =$lat ;
//error_log($lon . "->".gettype($JSonString['Places'][$i]['Location']['Latitude']), 3 , "/var/tmp/my-errors.log");
}