How to create dummy values in a collection in MongoDB

171 views
Skip to first unread message

joe nisha

unread,
Sep 4, 2013, 4:14:26 AM9/4/13
to mongod...@googlegroups.com
I dont know how to create dummy values in collections in MongoDB. In other words I want to know the mongo query that is equivalent the SQL query below

select id, "hello" as constval, 1 as no from sample;

Can someone please help me on this. 

Jeff Lee

unread,
Sep 4, 2013, 5:32:15 PM9/4/13
to mongod...@googlegroups.com
If your goal is to seed a collection with data you can do something like this:

> for (var i=1;i <= 10; i++){ db.foo.save({ _id:i, thename:Array(10).join('a') })}
> db.foo.find()
{ "_id" : 1, "thename" : "aaaaaaaaa" }
{ "_id" : 2, "thename" : "aaaaaaaaa" }
{ "_id" : 3, "thename" : "aaaaaaaaa" }
{ "_id" : 4, "thename" : "aaaaaaaaa" }
{ "_id" : 5, "thename" : "aaaaaaaaa" }
{ "_id" : 6, "thename" : "aaaaaaaaa" }
{ "_id" : 7, "thename" : "aaaaaaaaa" }
{ "_id" : 8, "thename" : "aaaaaaaaa" }
{ "_id" : 9, "thename" : "aaaaaaaaa" }
{ "_id" : 10, "thename" : "aaaaaaaaa" }


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

joe nisha

unread,
Sep 5, 2013, 7:01:15 AM9/5/13
to mongod...@googlegroups.com
Thanks for the reply. I dont want to add values or make modification to the collection, just want the values from the select query for internal processing as in the query
select id, "aaa" as num from test; 
through this query only the value appears in the query result but the table does not get modified. Is there anything equivalent in mongodb?

Jeff Lee

unread,
Sep 5, 2013, 11:34:56 AM9/5/13
to mongod...@googlegroups.com
The only ways that I'm aware of is to either use a bit of javascript to append to the document:

e.g.

> db.foods.find()
{ "_id" : ObjectId("5228a2750a59bf1df4bf4b7e"), "name" : "apple" }

> db.foods.find().forEach(function(x){x.type = 'fruit'; printjson(x);})
{
        "_id" : ObjectId("5228a2750a59bf1df4bf4b7e"),
        "name" : "apple",
        "type" : "fruit"
}

or with aggregation:

> db.foods.aggregate({$project:{_id:1, name:1, type:{$concat:'fruit'}}})
{
        "result" : [
                {
                        "_id" : ObjectId("5228a2750a59bf1df4bf4b7e"),
                        "name" : "apple",
                        "type" : "fruit"
                }
        ],
        "ok" : 1
}

There may be something easier.
Reply all
Reply to author
Forward
0 new messages