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"}})
Related
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})
Here is the problem,
I want to capitalise the first letter of a name in my existing
database, just wanted to know if there is any query so that i can make
it possbile.
what i want -- in my database so many names is in unformatted ways.. like lucy, Sean, jon and so on. i want to make them in a formatted ways like. Lucy. Sean, Jon. Can anyone help me with this?
Thanks in advance.
it may not be the best solution.
the only hiccup in below suggestion is to get "3" of $substr:["$name1",1,3] dynamically.
but gives you a start?
db.toupper.aggregate([{$project:{name:{$concat:[{$toUpper:{$substr:["$name1",0,1]}},{$substr:["$name1",1,**3**]}]}}}])
below is the result
db.toupper.find()
"_id" : ObjectId("5767ca0badb381a5cc0d19cd"), "name1" : "lean" }
"_id" : ObjectId("5767ca3aadb381a5cc0d19ce"), "name1" : "lean" }
db.toupper.aggregate([{$project:{name:{$concat:[{$toUpper:{$substr:["$name1",0,1]}},{$substr:["$name1",1,3]}]}}}])
"_id" : ObjectId("5767ca0badb381a5cc0d19cd"), "name" : "Lean" }
"_id" : ObjectId("5767ca3aadb381a5cc0d19ce"), "name" : "Lean" }
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"}).
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'
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.