MongoDB query to capitalise first letter in existing database - mongodb

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" }

Related

What is the correct way to use find().pretty() for this collection

I need to use find().pretty() to get the values for svalue and units but i am not sure how to write it correctly any help with syntax would be appreciated.
"IOT" : {
"measurements" : {
"meas_id" : "1",
"sensor_id" : "223344",
"svalue" : "22.3344",
"units" : "DEG",
"dt_measured" : "20190403T154653Z",
"lat" : "533244",
"long" : "60446.0"
}
To achieve expected result, use below option of find and use '0' to exclude column and '1' to include columns in output
db.measurements.find({},{"IOT.measurements.svalue":1,"IOT.measurements.units":1}).pretty()
You can do something like this
db.<table_name>.find({},{"IOT.measurements.svalue":"","IOT.measurements.units":""}).pretty()
This will return svalue and units along with _id only. if you dont want _id
u can set _id:false. Like this
db.<table_name>.find({},{_id:false,"IOT.measurements.svalue":"","IOT.measurements.units":""}).pretty()

how to find a value of a document nested in another document

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"}})

Find records with a value outside a specific range in MongoDB

I'm trying to find records in MongoDB that are created outside a specific period. The query to search for records inside a specific period is pretty straightforward:
db.test.find({"Published":{'$gt':"2011-08-02", '$lt':"2011-08-06"}})
So naturally, I tried this for "outside" a specific range:
db.test.find({'$not':{"Published":{'$gt':"2011-08-02", '$lt':"2011-08-06"}}})
But this returns an empty result, while there are definately records published then.
What query should I use instead? Can anyone help me? I'm using raw mongo queries.
Thanks in advance
--- UPDATE ---
I found that the following query works, but it doesn't look like the perfect solution:
db.test.find(
{'$or': [
{"Published":{'$lt':"2011-02-02"}},
{"Published":{'$gt':"2011-08-06"}}
]}
)
Is there a cleaner way to do it?
You are putting the $not in the wrong place. Try this:
db.test.find({"Published":{ $not:{$gt:"2011-08-02", $lt:"2011-08-06"} } })
For details, see the MongoDB docs about the $notoperator.
Edit as because of the comment this solution would not work:
> db.dates.find()
{ "_id" : ObjectId("5492d46ef6226b581c80c0a2"), "a" : 1, "date" : "2011-08-04" }
{ "_id" : ObjectId("5492d4e2f6226b581c80c0a3"), "a" : 2, "date" : "2011-08-07" }
> db.dates.find({date:{$not:{$gt:"2011-08-02",$lt:"2011-08-06"}}})
{ "_id" : ObjectId("5492d4e2f6226b581c80c0a3"), "a" : 2, "date" : "2011-08-07" }

MongoDB - How to find equals in collection and in embedded document

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.

In MongoDB, how does on get the value in a field for an embedded document, but query based on a different value

I have a basic structure like this:
> db.users.findOne()
{
"_id" : ObjectId("4f384903cd087c6f720066d7"),
"current_sign_in_at" : ISODate("2012-02-12T23:19:31Z"),
"current_sign_in_ip" : "127.0.0.1",
"email" : "something#gmail.com",
"encrypted_password" : "$2a$10$fu9B3M/.Gmi8qe7pXtVCPu94mBVC.gn5DzmQXH.g5snHT4AJSZYCu",
"last_sign_in_at" : ISODate("2012-02-12T23:19:31Z"),
"last_sign_in_ip" : "127.0.0.1",
"name" : "Trip Jameson",
"sign_in_count" : 100,
"usertimes" : [
...thousands and thousands of records like this one....
{
"enddate" : 348268392.115282,
"idle" : 0,
"startdate" : 348268382.116728,
"title" : "My Awesome Title"
},
]
}
So I want to find only usertimes for a single user where the title was "My Awesome Title", and then I want to see what the value for "idle" was in that record(s)
So far all I can figure out is that I can find the entire user record with a search like:
> db.users.find({'usertimes.title':"My Awesome Title"})
This just returns the entire User record though, which is useless for my purposes. Am I misunderstanding something?
Return only partial embedded documents is currently not supported by MongoDB
The matching User record will always be returned (at least with the current MongoDB version).
see this question for similar reference
Filtering embedded documents in MongoDB
This is the correspondent Jira on MongoDB space
http://jira.mongodb.org/browse/SERVER-142
Use:
db.users.find({'usertimes.title': "My Awesome Title"}, {'idle': 1});
May I suggest you take a more detailed look at http://www.mongodb.org/display/DOCS/Querying, it'll explain things for you.