Professional Documents
Culture Documents
Getting Started With Mon God B
Getting Started With Mon God B
The examples in this guide use the restaurants collection in the test database.
The following is a sample document in the restaurants collection:
"address": {
"building": "1007",
"zipcode": "10462"
},
"borough": "Bronx",
"cuisine": "Bakery",
"grades": [
],
"restaurant_id": "30075445"
}
Use the following procedure to populate the restaurants collection.
Prerequisites
You must have a running mongod instance in order to import data into the database.
Procedure
To import data into a mongod instance running on a different host or port, specify the
hostname or port by including the --host and the --port options in
your mongoimport command.
- 2 -
MongoDB Shell (mongo)
The mongo shell is an interactive JavaScript interface to MongoDB and is a component
of the MongoDB package. You can use the mongo shell to query and update data as
well as perform administrative operations.
Start mongo
Once you have installed and have started MongoDB, connect the mongo shell to your
running MongoDB instance. Ensure that MongoDB is running before attempting to
launch the mongo shell.
On the same system where the MongoDB is running, open a terminal window (or a
command prompt for Windows) and run the mongo shell with the following command:
mongo
mongo.exe
When you run mongo without any arguments, the mongo shell will attempt to connect to
the MongoDB instance running on the localhost interface on port 27017. To specify a
different host or port number, as well as other options, see mongo Shell Reference
Page.
Type help in the mongo shell for a list of available commands and their descriptions:
help
- 3 -
The mongo shell also provides <tab> key completion as well as keyboard shortcuts
similar to those found in the bash shell or in Emacs. For example, you can use the <up-
arrow> and the <down-arrow> to retrieve operations from its history.
You can use the insert() method to add documents to a collection in MongoDB. If
you attempt to add documents to a collection that does not exist, MongoDB will create
the collection for you.
Prerequisites
In the mongo shell connected to a running mongod instance, switch to the test database.
use test
Insert a Document
Insert a document into a collection named restaurants. The operation will create the
collection if the collection does not currently exist.
db.restaurants.insert(
"address" : {
"zipcode" : "10075",
"building" : "1480",
},
"borough" : "Manhattan",
"cuisine" : "Italian",
- 4 -
"grades" : [
"date" : ISODate("2014-10-01T00:00:00Z"),
"grade" : "A",
"score" : 11
},
"date" : ISODate("2014-01-16T00:00:00Z"),
"grade" : "B",
"score" : 17
],
"name" : "Vella",
"restaurant_id" : "41704620"
The method returns a WriteResult object with the status of the operation.
WriteResult({ "nInserted" : 1 })
If the document passed to the insert() method does not contain the _id field,
the mongo shell automatically adds the field to the document and sets the field’s value
to a generated ObjectId.
- 5 -
Find or Query Data with the mongo Shell
Overview
You can use the find() method to issue a query to retrieve data from a collection in
MongoDB. All queries in MongoDB have the scope of a single collection.
Queries can return all documents in a collection or only the documents that match a
specified filter or criteria. You can specify the filter or criteria in a document and pass
as a parameter to the find() method.
The find() method returns query results in a cursor, which is an iterable object that
yields documents.
Prerequisites
The examples in this section use the restaurants collection in the test database. For
instructions on populating the collection with the sample dataset, see Import Example
Dataset.
In the mongo shell connected to a running mongod instance, switch to the test database.
use test
To return all documents in a collection, call the find() method without a criteria
document. For example, the following operation queries for all documents in
the restaurants collection.
db.restaurants.find()
- 6 -
Specify Equality Conditions
The query condition for an equality match on a field has the following form:
If the <field> is a top-level field and not a field in an embedded document or an array,
you can either enclose the field name in quotes or omit the quotes.
If the <field> is in an embedded document or an array, use dot notation to access the
field. With dot notation, you must enclose the dotted name in quotes.
- 7 -
documents whose grades array contains an embedded document with a
field gradeequal to "B".
- 8 -
Combine Conditions
You can combine multiple query conditions in logical conjunction (AND) and logical
disjunctions (OR).
Logical AND
You can specify a logical conjunction (AND) for a list of query conditions by separating
the conditions with a comma in the conditions document.
The result set includes only the documents that matched all specified criteria.
Logical OR
You can specify a logical disjunction (OR) for a list of query conditions by using
the $or query operator.
db.restaurants.find(
The result set includes only the documents that match either conditions.
To specify an order for the result set, append the sort() method to the query. Pass
to sort() method a document which contains the field(s) to sort by and the
corresponding sort type, e.g. 1 for ascending and -1for descending.
- 9 -
The operation returns the results sorted in the specified order.
You can use the update() method to update documents of a collection. The method
accepts as its parameters:
To specify the filter, use the same structure and syntax as the query conditions.
See Find or Query Data with the mongo Shell for an introduction to query conditions.
By default, the update() method updates a single document. Use the multi option to
update all documents that match the criteria.
Prerequisites
The examples in this section use the restaurants collection in the test database. For
instructions on populating the collection with the sample dataset, see Import Example
Dataset.
In the mongo shell connected to a running mongod instance, switch to the test database.
use test
- 10 -
Update Specific Fields
To change a field value, MongoDB provides update operators, such as $set to modify
values. Some update operators, such as $set, will create the field if the field does not
exist. See the individual update operatorsreference.
db.restaurants.update(
{ "name" : "Juni" },
The update operation returns a WriteResult object which contains the status of the
operation.
db.restaurants.update(
{ "restaurant_id" : "41156888" },
The update operation returns a WriteResult object which contains the status of the
operation.
- 11 -
Update Multiple Documents
By default, the update() method updates a single document. To update multiple
documents, use themulti option in the update() method. The following operation
updates all documents that haveaddress.zipcode field equal
to "10016" and cuisine field equal to "Other", setting the cuisinefield
to "Category To Be Determined" and the lastModified field to the current date.
db.restaurants.update(
},
{ multi: true}
The update operation returns a WriteResult object which contains the status of the
operation.
Replace a Document
To replace the entire document except for the _id field, pass an entirely new
document as the second argument to the update() method. The replacement
document can have different fields from the original document. In the replacement
document, you can omit the _id field since the _id field is immutable. If you do include
the _id field, it must be the same value as the existing value.
IMPORTANT
After the update, the document only contains the field or fields in the replacement
document.
After the following update, the modified document will only contain
the _id field, name field, the addressfield. i.e. the document will not contain
the restaurant_id, cuisine, grades, and the borough fields.
- 12 -
db.restaurants.update(
{ "restaurant_id" : "41704620" },
"address" : {
"building" : "1480",
"zipcode" : "10075"
The update operation returns a WriteResult object which contains the status of the
operation.
- 13 -