{ type : "Point" , coordinates : [ lng , lat ] }
{ type : "LineString" , coordinates : [ [ 40 , 5 ] , [ 41 , 6 ] ] }
Polygons with a Single Ring
{
type : "Polygon" ,
coordinates : [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]
}
Polygons with Multiple Rings
{
type : "Polygon" ,
coordinates : [
[ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ,
[ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]
]
}
{
type : "MultiPoint" ,
coordinates : [
[ - 73.9580 , 40.8003 ] ,
[ - 73.9498 , 40.7968 ] ,
[ - 73.9737 , 40.7648 ] ,
[ - 73.9814 , 40.7681 ]
]
}
{
type : "MultiLineString" ,
coordinates : [
[ [ - 73.96943 , 40.78519 ] , [ - 73.96082 , 40.78095 ] ] ,
[ [ - 73.96415 , 40.79229 ] , [ - 73.95544 , 40.78854 ] ] ,
[ [ - 73.97162 , 40.78205 ] , [ - 73.96374 , 40.77715 ] ] ,
[ [ - 73.97880 , 40.77247 ] , [ - 73.97036 , 40.76811 ] ]
]
}
{
type : "MultiPolygon" ,
coordinates : [
[ [ [ - 73.958 , 40.8003 ] , [ - 73.9498 , 40.7968 ] , [ - 73.9737 , 40.7648 ] , [ - 73.9814 , 40.7681 ] , [ - 73.958 , 40.8003 ] ] ] ,
[ [ [ - 73.958 , 40.8003 ] , [ - 73.9498 , 40.7968 ] , [ - 73.9737 , 40.7648 ] , [ - 73.958 , 40.8003 ] ] ]
]
}
{
type : "GeometryCollection" ,
geometries : [
{
type : "MultiPoint" ,
coordinates : [
[ - 73.9580 , 40.8003 ] ,
[ - 73.9498 , 40.7968 ] ,
[ - 73.9737 , 40.7648 ] ,
[ - 73.9814 , 40.7681 ]
]
} ,
{
type : "MultiLineString" ,
coordinates : [
[ [ - 73.96943 , 40.78519 ] , [ - 73.96082 , 40.78095 ] ] ,
[ [ - 73.96415 , 40.79229 ] , [ - 73.95544 , 40.78854 ] ] ,
[ [ - 73.97162 , 40.78205 ] , [ - 73.96374 , 40.77715 ] ] ,
[ [ - 73.97880 , 40.77247 ] , [ - 73.97036 , 40.76811 ] ]
]
}
]
}
Geospatial Indexes and Sharded Collections
Geospatial Query Operators
Geospatial Aggregation Stage
Each BSON type has both integer and string identifiers as listed in the following table:
Type
Number
Alias
Notes
Double
1
“double”
String
2
“string”
Object
3
“object”
Array
4
“array”
Binary data
5
“binData”
Undefined
6
“undefined”
Deprecated.
ObjectId
7
“objectId”
new ObjectId
Boolean
8
“bool”
Date
9
“date”
The mongo
shell provides various methods to return the date, either as a string or as a Date
object: - Date()
method which returns the current date as a string. - new Date()
constructor which returns a Date
object using the ISODate()
wrapper. - ISODate()
constructor which returns a Date
object using the ISODate()
wrapper.
Null
10
“null”
Regular Expression
11
“regex”
DBPointer
12
“dbPointer”
Deprecated.
JavaScript
13
“javascript”
Symbol
14
“symbol”
Deprecated.
JavaScript (with scope)
15
“javascriptWithScope”
32-bit integer
16
“int”
NumberInt()
Timestamp
17
“timestamp”
64-bit integer
18
“long”
NumberLong("2090845886852")
Decimal128
19
“decimal”
NumberDecimal("1000.55")
Min key
-1
“minKey”
Max key
127
“maxKey”
Check Types in the mongo Shell
# to stop the mongodb service
net stop MongoDB
# start the mongo service
mongod
# to change the default directory
mongod --dbpath " C:\data\db"
# start MongoDB shell using the non-default MongoDB Server port
mongo --port
# print the working db
db
Help Methods and Commands
Description
help
Show help.
db.help()
Show help for database methods.
db.<collection>.help()
Show help on collection methods. The <collection>
can be the name of an existing collection or a non-existing collection.
show dbs
Print a list of all databases on the server.
show databases
Print a list of all available databases.
use <db>
Switch current database to <db>
. The mongo
shell variable db
is set to the current database.
show collections
Print a list of all collections for current database.
show users
Print a list of users for current database.
show roles
Print a list of all roles, both user-defined and built-in, for the current database.
show profile
Print the five most recent operations that took 1 millisecond or more. See documentation on the database profiler for more information.
load()
Execute a JavaScript file. See Write Scripts for the mongo Shell for more information.
Name
Syntax
Example
db.collection.insert()
db.collection.insert(
<document or array of documents>,
{
writeConcern: <document>,
ordered: <boolean>
}
)
db.collection.insertOne()
db.collection.insertOne(
<document>,
{ writeConcern: <document> }
)
db.collection.insertMany()
db.collection.insertMany(
[ <document 1> , <document 2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>
}
)
db.collection.find()
db.collection.find(
<query>,
<projection>
)
db.bios.find(
{
birth: { $gt: new Date('1920-01-01') },
death: { $exists: false }
} ,
{
firstName: 1,
lastname:1,
_id: 0
}
)
db.collection.findOne()
db.collection.findOne(
<query>,
<projection>
)
db.bios.findOne({
$or: [
{ 'name.first' : /^G/ },
{ birth: { $lt: new Date('01/01/1945') } }
]
})
db.collection.updateOne()
db.collection.updateOne(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdoc1>, ... ],
hint: <document|string>
}
)
db.restaurant.updateOne(
{ "name" : "Central Perk Cafe" },
{ $set: { "violations" : 3 } }
);
db.collection.updateMany()
db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdoc1>, ... ],
hint: <document|string>
}
)
db.collection.update()
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdoc1>, ... ],
hint: <document|string>
}
)
db.books.update(
{ item: "ZZZ135" },
{
item: "ZZZ135",
stock: 5,
tags: [ "database" ]
},
{ upsert: true }
)
db.collection.replaceOne()
db.collection.replaceOne(
<filter>,
<replacement>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
hint: <document|string>
}
)
db.collection.deleteOne()
db.collection.deleteOne(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)
db.collection.deleteMany()
db.collection.deleteMany(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)
db.collection.remove()
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>,
collation: <document>
}
)
Name
Syntax
Description
db.createCollection()
db.createCollection( <name>, { capped: <boolean>, autoIndexId: <boolean>, size: <number>, max: <number>, storageEngine: <document>, validator: <document>, validationLevel: <string>, validationAction: <string>, indexOptionDefaults: <document>, viewOn: <string>, pipeline: <pipeline>, collation: <document>, writeConcern: <document> } )
Creates a new collection or a view. Commonly used to create a capped collection.
db.dropDatabase()
Removes the current database.
db.runCommand()
Runs a database command .
db.shutdownServer()
Shuts down the current mongod
or mongos
process cleanly and safely.
db.stats()
Returns a document that reports on the state of the current database.
Name
Description
cursor.pretty()
Configures the cursor to display results in an easy-to-read format.
cursor.count()
Modifies the cursor to return the number of documents in the result set rather than the documents themselves.
cursor.limit()
Constrains the size of a cursor’s result set.
cursor.forEach()
Applies a JavaScript function for every document in a cursor.
cursor.map()
Applies a function to each document in a cursor and collects the return values in an array.
cursor.next()
Returns the next document in a cursor.
cursor.hasNext()
Returns true if the cursor has documents and can be iterated.
cursor.skip()
Returns a cursor that begins returning results only after passing or skipping a number of documents.
cursor.sort()
Returns results ordered according to a sort specification.
cursor.toArray()
Returns an array that contains all documents returned by the cursor.
Name
Verbose
Description
$eq
equal
Matches values that are equal to a specified value.
$ne
not equal
Matches all values that are not equal to a specified value.
$gt
greater than
Matches values that are greater than a specified value.
$gte
greater than or equal
Matches values that are greater than or equal to a specified value.
$lt
less than
Matches values that are less than a specified value.
$lte
less than or equal
Matches values that are less than or equal to a specified value.
$in
in array
Matches any of the values specified in an array.
$nin
not in array
Matches none of the values specified in an array.
Name
Description
$and
Joins query clauses with a logical AND
returns all documents that match the conditions of both clauses.
$or
Joins query clauses with a logical OR
returns all documents that match the conditions of either clause.
$not
Inverts the effect of a query expression and returns documents that do not match the query expression.
$nor
Joins query clauses with a logical NOR
returns all documents that fail to match both clauses.
Name
Description
$exists
Matches documents that have the specified field.
$type
Selects documents if a field is of the specified type.
Name
Description
$expr
Allows use of aggregation expressions within the query language.
$jsonSchema
Validate documents against the given JSON Schema.
$mod
Performs a modulo operation on the value of a field and selects documents with a specified result.
$regex
Selects documents where values match a specified regular expression.
$text
Performs text search.
$where
Matches documents that satisfy a JavaScript expression.
Name
Description
$all
Matches arrays that contain all elements specified in the query.
$elemMatch
Selects documents if element in the array field matches all the specified $elemMatch
conditions.
$size
Selects documents if the array field is a specified size.
Name
Description
$comment
Adds a comment to a query predicate.
Name
Description
$
Projects the first element in an array that matches the query condition.
$elemMatch
Projects the first element in an array that matches the specified $elemMatch
condition.
$meta
Projects the document’s score assigned during $text
operation.
$slice
Limits the number of elements projected from an array. Supports skip and limit slices.
Name
Description
$set
Sets the value of a field in a document.
$unset
Removes the specified field from a document.
$inc
Increments the value of the field by the specified amount.
$min
Only updates the field if the specified value is less than the existing field value.
$max
Only updates the field if the specified value is greater than the existing field value.
$mul
Multiplies the value of the field by the specified amount.
$rename
Renames a field.
Name
Description
$
Acts as a placeholder to update the first element that matches the query condition.
$[]
Acts as a placeholder to update all elements in an array for the documents that match the query condition.
$[<identifier>]
Acts as a placeholder to update all elements that match the arrayFilters
condition for the documents that match the query condition.
$push
Adds an item to an array.
$addToSet
Adds elements to an array only if they do not already exist in the set.
$pull
Removes all array elements that match a specified query.
$pullAll
Removes all matching values from an array.
$pop
Removes the first or last item of an array.
Name
Description
$each
Modifies the $push
and $addToSet
operators to append multiple items for array updates.
$slice
Modifies the $push
operator to limit the size of updated arrays.
$position
| Modifies the $push
operator to specify the position in the array to add elements.
$sort
| Modifies the $push
operator to reorder documents stored in an array.