Database Reference
In-Depth Information
db.book.insert( { titleName : “Data Modeling for MongoDB” } )
If the book collection does not exist, it will be created when adding this first document.
The insert statement will also lead to the creation of an ObjectId field to identify this new
document.
Multiple documents can be added to a collection with one command by using an array:
db.book.insert( [ { titleName : “Data Modeling Made Simple” },
{ titleName : “Extreme Scoping” },
{ titleName : “UML Database Modeling Workbook” }
] )
To see what we just created, use the find command, which we'll cover in the next section:
db.book.find( )
And this is what is returned:
{ “_id” : ObjectId(“530f8be4d77a08823086017c”), “titleName” : “Data Modeling for MongoDB” }
{ “_id” : ObjectId(“530f8be4d77a08823086017d”), “titleName” : “Data Modeling Made Simple” }
{ “_id” : ObjectId(“530f8be4d77a08823086017e”), “titleName” : “Extreme Scoping” }
{ “_id” : ObjectId(“530f8be4d77a08823086017f”), “titleName” : “UML Database Modeling Workbook” }
Note the ObjectIds were automatically created for us. We could optionally specify values
for each ObjectId field. ObjectId is a default that's good for guaranteeing uniqueness.
However, if there are already unique keys in your data, you can use these unique keys for
ObjectId values and this will save you an extra index.
The previous example illustrates when a document just contains the ObjectID and the
book's title. But what if we have more fields such as the three topics in this spreadsheet?
Page
Count
Title Name
Subtitle Name
Categories
An Agile Approach to Enterprise Data Warehousing
and Business Intelligence
Agile, Business Intelli-
gence
Extreme Scoping
300
Search WWH ::




Custom Search