Inserting data

Since we are working with the chapter3 database, which is a brand new database, there are currently no collections in it. You can use any collection (table) you want by simply referring to a new collection name with the db object:

> db.newCollection.find() 
> 

Performing a find operation on an empty collection simply returns nothing. Let's insert some data so we can experiment with some queries:

> db.newCollection.insert({ name: 'Jason Krol', website: 
'http://kroltech.com' }) > db.newCollection.find().pretty() { "_id" : ObjectId("5338b749dc8738babbb5a45a"), "name" : "Jason Krol", "website" : "http://kroltech.com" }

After we perform a simple insertion (basically of a JavaScript JSON object), we will perform another find operation on the collection and get our new record returned, this time with an additional _id field added. The _id field is Mongo's method for tracking a unique identifier for every document (record). We also chained the pretty() function to the end of the find(), which outputs the results a little more nicely.

Go ahead and insert a few more records, so you have some data to play with for the next section when we go over querying.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.51.228