how to convert field to lower case in script?

9,661 views
Skip to first unread message

rl

unread,
Mar 17, 2012, 9:29:04 PM3/17/12
to mongod...@googlegroups.com
I am trying to create a lower case version of a field in all my documents.  I tried toLower(), toLowerCase(), $toLower, etc.. but none work.  I searched around and couldn't find any docs on how to do it (at least correct documentation that worked)

db.mydb.find().forEach(function(doc) {
doc.lname = doc.name.toLower();
db. mydb.save(doc);
})

TypeError: doc.name.toLower is not a function

Wes Freeman

unread,
Mar 17, 2012, 9:39:42 PM3/17/12
to mongod...@googlegroups.com
You said you tried it, but toLowerCase() worked for me.

> db.test.save({name:"TEST"});
> db.test.find().forEach(function(doc) {
... doc.lname = doc.name.toLowerCase();
... db.test.save(doc);
... });
> db.test.find();
{ "_id" : ObjectId("4f653c644b0a7cc12e0b3ea3"), "name" : "TEST",
"lname" : "test" }

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/AqXr_MT_eo0J.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.

rl

unread,
Mar 17, 2012, 9:58:59 PM3/17/12
to mongod...@googlegroups.com

It's still not working for me...

MongoDB shell version: 2.0.3
connecting to: test
> db.mydb.find().forEach(function(doc) {
...  doc.lname = doc.name.toLowerCase();
...  db.mydb.save(doc);
... });
Sun Mar 18 01:57:49 TypeError: doc.name.toLowerCase is not a function (shell):2


Scott Hernandez

unread,
Mar 17, 2012, 10:03:23 PM3/17/12
to mongod...@googlegroups.com
Is name a string in *all* your docs?

You may want to check it before calling toLowerCase.

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/mongodb-user/-/nedbnILEeAEJ.

rl

unread,
Mar 17, 2012, 10:09:16 PM3/17/12
to mongod...@googlegroups.com
It should be.  How would I test it to be sure?

FYI - I tried this:

> db.mydb.find({"name":null}).limit(1);

which returned 0 records...

rl

unread,
Mar 17, 2012, 10:13:06 PM3/17/12
to mongod...@googlegroups.com
I am running this query now.  I assume this is what you wanted me to try?

> db.mydb.find( { name : { $not: { $type : 2 } }} );

Wes Freeman

unread,
Mar 17, 2012, 10:20:36 PM3/17/12
to mongod...@googlegroups.com
You can either do something like this:

if(doc.name != null) doc.lname = doc.name.toLowerCase();

or make the query like this:

db.mydb.find({name:{$exists:true}});

Wes

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/mongodb-user/-/h38QYazOocgJ.

Wes Freeman

unread,
Mar 17, 2012, 10:23:17 PM3/17/12
to mongod...@googlegroups.com
You could also force it to be a string with something like this:
var strName = ""+doc.name;
doc.lname = strName.toLowerCase();

H.J

unread,
Mar 17, 2012, 10:45:50 PM3/17/12
to mongod...@googlegroups.com
It would not caused by null value, but another types, such as
db.blog.insert({"name":["TEST",123]})
or
db.blog.insert({name:{id:"ABC",tags:"EFG"}})

follow is testing report:
> db.blog.insert({"name":["TEST",123]})
> db.blog.find().forEach(function(doc){
... doc.name = doc.name.toLowerCase();
... db.blog.save(doc);
... })
Sun Mar 18 10:32:51 TypeError: doc.name.toLowerCase is not a function (shell):2
> db.blog.remove()
> db.blog.insert({"name":null})
> db.blog.find().forEach(function(doc){
... doc.name = doc.name.toLowerCase();
... db.blog.save(doc);
... })
Sun Mar 18 10:33:18 TypeError: doc.name has no properties (shell):2


2012/3/18 Wes Freeman <freem...@gmail.com>:

--
红颜弹指老,刹那芳华!

H.J

unread,
Mar 17, 2012, 11:16:11 PM3/17/12
to mongod...@googlegroups.com
strange that
{ name : { $not: { $type : 2 } }}
or
{ name : { $type : 4 } }
can't find the document
{"name":["TEST",123]}

any idea?

2012/3/18 H.J <shiy...@gmail.com>:

--
红颜弹指老,刹那芳华!

Wes Freeman

unread,
Mar 17, 2012, 11:27:53 PM3/17/12
to mongod...@googlegroups.com
It shows up in {name: {$type:2}} -- weird.

This works, however:
> db.test.find().forEach(function(doc) {
... if(doc.name != null && typeof doc.name == "string") doc.lname =
doc.name.toLowerCase();
... db.test.save(doc);
... });

Wes

Reply all
Reply to author
Forward
0 new messages