Monday, June 25, 2012

Mongo tutorial!

http://www.mongodb.org/display/DOCS/Tutorial tells us...

 
 

j = { name : "mongo" };
db.things.save(j);

...seemed to create...

( "_id" : ObjectId("4fe8b073e4dada0af3bde91e"), "name" : "mongo" )

...per what comes back from...

db.things.find();

 
 

for (var i = 1; i <= 25; i++) db.things.save({x : 4, j : i});

...will add numerous records and when one uses...

db.things.find();

...to retrieve them one will only see the first twenty with...

has more

...at the bottom of the dump. Typing...

it

...will give the next records.

 
 

var cursor = db.things.find();
while (cursor.hasNext()) printjson(cursor.next());

...should show everything in the cursor all at once.

 
 

db.things.findOne({name:"mongo"});

...retrieves the one record we created with a name of mongo.

 
 

k = { name : "foo", other "bar" };
l = { name : "foo", other "baz" };
db.things.save(k);
db.things.save(l);
db.things.findOne({name:"foo"});

...returns only first of the two foo records while...

db.things.find({name:"foo"});

...returns a complete collection.

 
 

var cursor = db.things.find();
cursor[4];

...will return the appropriate item from the array.

 
 

db.things.findOne({_id:ObjectId("4fe8b073e4dada0af3bde91e")});

...shows finding by Guid.

 
 

db.things.find({name:"foo"}, {other:"bar"});

...will find records where either name is "foo" or other is "bar" and NOT records that meet BOTH conditions.

 
 

db.things.find({name:"foo", other:"bar"});

...will find records where BOTH name is "foo" or other is "bar"

 
 

db.things.find().limit(3);

...will constrain what is returned.

No comments:

Post a Comment