πŸ“œ ⬆️ ⬇️

Mongodb 3.2 release some details


Recently a new stable release mongodb released. In this version, a number of innovations were added, such as a new GUI for visual work with mongodb , LEFT JOIN , document validation, etc. we will look at some of these properties with small examples below.


Partial (partial) index


MongoDB 3.2 provides the ability to create indexes only for those collection documents that will match the specified filter. Indexing only parts of the required documents in the collection has a number of advantages, such as lower storage requirements, and lower performance costs, to create and maintain an index. You can specify the partialFilterExpression option for all MongoDB index types .

To create a partial index, use the db.collection.createIndex() method with the new partialFilterExpression option. In the following example, we will create a composite index that will index only those documents that have a rating greater than 5.
 db.restaurants.createIndex( { cuisine: 1, name: 1 }, { partialFilterExpression: { rating: { $gt: 5 } } } ) 

Comparison with sparse index.


A partial index provides a larger set of capabilities than a sparse index and is therefore preferable. The partial index offers a more expressive mechanism for specifying which documents are better to index.
Sparse indexes select documents to be indexed solely based on the presence of an indexed field.
An example of a sparse index behavior using a partial index:
 db.contacts.createIndex( { name: 1 }, { partialFilterExpression: { name: { $exists: true } } } ) 


Consider another example with a partial index, here the field by which the name will be indexed, and the field by which documents will be filtered, another:
 db.contacts.createIndex( { name: 1 }, { partialFilterExpression: { email: { $exists: true } } } ) 

The following query can use an index:
 db.contacts.find( { name: "xyz", email: { $regex: /\.org$/ } } ) 

And for this request, the index can not be used:
 db.contacts.find( { name: "xyz", email: { $exists: false } } ) 

Restrictions


In mongodb you cannot create multiple versions of an index that differ only in parameters. You cannot create many partial indexes that differ only in filter parameters.
In earlier versions of mongodb , partial indexes were not supported. All sharded clusters, replica sets and all nodes should work on mongodb 3.2 .
_id index cannot be a partial index.
The key of the shard also cannot be a partial index.
')
Consider a collection of restaurants that contains the following documents and looks like this:
 { "_id" : ObjectId("5641f6a7522545bc535b5dc9"), "address" : { "building" : "1007", "coord" : [ -73.856077, 40.848447 ], "street" : "Morris Park Ave", "zipcode" : "10462" }, "borough" : "Bronx", "cuisine" : "Bakery", "rating" : { "date" : ISODate("2014-03-03T00:00:00Z"), "grade" : "A", "score" : 2 }, "name" : "Morris Park Bake Shop", "restaurant_id" : "30075445" } 

We could add an index to the borough and cuisine fields by filtering only those documents where rating.grade == A
 db.restaurants.createIndex( { borough: 1, cuisine: 1 }, { partialFilterExpression: { 'rating.grade': { $eq: "A" } } } ) 

The following query for a collection of restaurants using a partial index will return all the restaurants in Bronx where rating.grade equivalent to A
 db.restaurants.find( { borough: "Bronx", 'rating.grade': "A" } ) 

And this query will not be able to use the index, because there are no fields corresponding to rating.grade
 db.restaurants.find( { borough: "Bronx", cuisine: "Bakery" } ) 

Partial uniqueness index


A partial index can be applied to documents that contain a unique constraint .

For example, take the users collection containing the following documents:
 { "_id" : ObjectId("56424f1efa0358a27fa1f99a"), "username" : "david", "age" : 29 } { "_id" : ObjectId("56424f37fa0358a27fa1f99b"), "username" : "amanda", "age" : 35 } { "_id" : ObjectId("56424fe2fa0358a27fa1f99c"), "username" : "rajiv", "age" : 57 } 

The following operation will create an index that imposes a unique restriction on the username field and a partial filter for the expression age: { $gte: 21 }
 db.users.createIndex( { username: 1 }, { unique: true, partialFilterExpression: { age: { $gte: 21 } } } ) 

The index prevents the insertion of the following documents, because documents with the specified name already exist and the age field is greater than 21:
 db.users.insert( { username: "david", age: 27 } ) db.users.insert( { username: "amanda", age: 25 } ) db.users.insert( { username: "rajiv", age: 32 } ) 

And these documents will be inserted:
 db.users.insert( { username: "david", age: 20 } ) db.users.insert( { username: "amanda" } ) db.users.insert( { username: "rajiv", age: null } ) 


Validation


Starting with version 3.2 , mongodb provides the ability to validate documents during the update and paste process. Validation rules are specified when an explicit collection is created using the validator option, or using the collMod command for an existing collection. The validator option accepts a document that matches certain expressions, with the exception of $geoNear , $near , $nearSphere , $text and $where .

Consider the following example of creating a validation when creating a contacts collection:
 db.createCollection( "contacts", { validator: { $or: [ { phone: { $type: "string" } }, { email: { $regex: /@mongodb\.com$/ } }, { status: { $in: [ "Unknown", "Incomplete" ] } } ] } } ) 

MongoDB also provides a validationLevel option that determines how strictly MongoDB applies validation rules for existing documents during their update, and the validationAction option that determines whether mongodb should reject documents that violate validation rules.

Behavior


Validation occurs during the update and insertion of documents. When adding validations to a collection, existing documents are not modified.

Existing documents


You can control how mongodb processes existing documents using the validationLevel option.
By default, validationLevel is strict , and mongodb applies validation rules for all inserts and updates. If validationLevel set to moderate , then validation rules for inserting and updating documents apply only to documents that fully meet the validation criteria.

Consider the following documents in the contacts collection:
 { "_id": "125876" "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" }, { "_id": "860000", "name": "Ivan", "city": "Vancouver" } 

Run the command to add validation to the contacts collection:
 db.runCommand( { collMod: "contacts", validator: { $or: [ { phone: { $exists: true } }, { email: { $exists: true } } ] }, validationLevel: "moderate" } ) 

Now the contacts collection has a validator. And now if we update the document with {_id: 125876} , mongodb will apply the validation rules, since the existing document meets the criteria. And for the document { _id:860000 } , mongodb will not apply validation rules for updates, since the document does not match the validation rule.
If we need to disable validation, we can set the validationLevel option to off .

Accept or reject defective documents

In the validationAction option, it is defined how to deal with documents that violated the validation rules.
By default, validationAction is set to error and MongoDB rejects any insertions and updates of documents, if there is a violation of the validation rule. When validationAction set to warn , MongoDB logs documents, but allows inserts and updates.

The following example creates a contacts collection with a validator indicating that inserted or updated documents must meet at least one of the following three conditions:
  • the phone field must be a string
  • The email field must be checked by regular expressions for consistency.
  • The status field must be either Unknown or Incomplete.

 db.createCollection( "contacts", { validator: { $or: [ { phone: { $type: "string" } }, { email: { $regex: /@mongodb\.com$/ } }, { status: { $in: [ "Unknown", "Incomplete" ] } } ], validationAction: "warn" } } ) 

The next insert should fail, but since the validation rule validationAction warn , .
2015-10-15T11:20:44.260-0400 W STORAGE [conn3] Document would fail validation collection: example.contacts doc: { _id: ObjectId('561fc44c067a5d85b96274e4'), name: "Amanda", status: "Updated" }
validationAction warn , .
2015-10-15T11:20:44.260-0400 W STORAGE [conn3] Document would fail validation collection: example.contacts doc: { _id: ObjectId('561fc44c067a5d85b96274e4'), name: "Amanda", status: "Updated" }
 validationAction   warn ,         . 
2015-10-15T11:20:44.260-0400 W STORAGE [conn3] Document would fail validation collection: example.contacts doc: { _id: ObjectId('561fc44c067a5d85b96274e4'), name: "Amanda", status: "Updated" }
validationAction warn , .
2015-10-15T11:20:44.260-0400 W STORAGE [conn3] Document would fail validation collection: example.contacts doc: { _id: ObjectId('561fc44c067a5d85b96274e4'), name: "Amanda", status: "Updated" }

Restrictions


You cannot assign validation to admin , local .
And you cannot specify a validator for system.* Collections

Innovations of the aggregation framework and LEFT JOIN


$lookup - linking multiple collections.
Although this operator called their left join for mongodb it still seems to me that this question is a bit wider, and it should be considered in the context of linking different types of documents, not collections. I described this question in some detail here .
$lookup has the following syntax:
 { $lookup: { from: <  >, localField: <   >, foreignField: <     >, as: < > } } 

In the example we have two collections:
The orders collection contains the following documents:
 { "_id" : 1, "item" : "abc", "price" : 12, "quantity" : 2 } { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1 } { "_id" : 3 } 

And the inventory collection containing documents:
 { "_id" : 1, "sku" : "abc", description: "product 1", "instock" : 120 } { "_id" : 2, "sku" : "def", description: "product 2", "instock" : 80 } { "_id" : 3, "sku" : "ijk", description: "product 3", "instock" : 60 } { "_id" : 4, "sku" : "jkl", description: "product 4", "instock" : 70 } { "_id" : 5, "sku": null, description: "Incomplete" } { "_id" : 6 } 

The following aggregation operation, for the orders collection, links documents from orders to documents from the inventory collection, using the item field from orders and the sku field from inventory :
 db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } } ]) 

At the exit, this operation will return the following documents:
 { "_id" : 1, "item" : "abc", "price" : 12, "quantity" : 2, "inventory_docs" : [ { "_id" : 1, "sku" : "abc", description: "product 1", "instock" : 120 } ] } { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "inventory_docs" : [ { "_id" : 4, "sku" : "jkl", "description" : "product 4", "instock" : 70 } ] } { "_id" : 3, "inventory_docs" : [ { "_id" : 5, "sku" : null, "description" : "Incomplete" }, { "_id" : 6 } ] } 

$sample - randomly selects a specified number of documents from the input stream.
Syntax:
 { $sample: { size: <positive integer> } } 

In the example we have a collection of users consisting of the following documents:
 { "_id" : 1, "name" : "dave123", "q1" : true, "q2" : true } { "_id" : 2, "name" : "dave2", "q1" : false, "q2" : false } { "_id" : 3, "name" : "ahn", "q1" : true, "q2" : true } { "_id" : 4, "name" : "li", "q1" : true, "q2" : false } { "_id" : 5, "name" : "annT", "q1" : false, "q2" : true } { "_id" : 6, "name" : "li", "q1" : true, "q2" : true } { "_id" : 7, "name" : "ty", "q1" : false, "q2" : true } 

The aggregation operation will randomly select three documents from the collection:
 db.users.aggregate( [ { $sample: { size: 3 } } ] ) 

PostgreSQL 9.5 , which is about to be released, also has a similar feature.

$indexStats - returns usage statistics for each index.
Returns usage statistics for each index in the collection. If running with access control, the user must have privileges that include indexStats .

Syntax:
 { $indexStats: { } } 

For example, take the orders collection containing the following documents:
 { "_id" : 1, "item" : "abc", "price" : 12, "quantity" : 2, "type": "apparel" } { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "type": "electronics" } { "_id" : 3, "item" : "abc", "price" : 10, "quantity" : 5, "type": "apparel" } 

Create the following two indices on the collection:
 db.orders.createIndex( { item: 1, quantity: 1 } ) db.orders.createIndex( { type: 1, item: 1 } ) 

Perform some queries:
 db.orders.find( { type: "apparel"} ) db.orders.find( { item: "abc" } ).sort( { quantity: 1 } ) 

To view statistics on the use of the index for the orders collection, perform the following operation:
 db.orders.aggregate( [ { $indexStats: { } } ] ) 

The operation will return documents containing the use of statistics for each index:
 { "name" : "item_1_quantity_1", "key" : { "item" : 1, "quantity" : 1 }, "host" : "examplehost.local:27017", "accesses" : { "ops" : NumberLong(1), "since" : ISODate("2015-10-02T14:31:53.685Z") } } { "name" : "_id_", "key" : { "_id" : 1 }, "host" : "examplehost.local:27017", "accesses" : { "ops" : NumberLong(0), "since" : ISODate("2015-10-02T14:31:32.479Z") } } { "name" : "type_1_item_1", "key" : { "type" : 1, "item" : 1 }, "host" : "examplehost.local:27017", "accesses" : { "ops" : NumberLong(1), "since" : ISODate("2015-10-02T14:31:58.321Z") } } 


$stdDevSamp β€” $stdDevSamp standard deviation of a sample of input values.
It is accessible from $group and $project , ignores non-numeric values ​​and, if such is provided, returns null .
For example, take the users collection:
 {_id: 0, username: "user0", age: 20} {_id: 1, username: "user1", age: 42} {_id: 2, username: "user2", age: 28} 

To calculate the standard deviation of a random sample of users, first use the $sample operator to sample 100 users, and then use $stdDevSamp for costing.
 db.users.aggregate( [ { $sample: { size: 100 } }, { $group: { _id: null, ageStdDev: { $stdDevSamp: "$age" } } } ] ) 

Result:
 { "_id" : null, "ageStdDev" : 7.811258386185771 } 


New aggregation arithmetic operators


$sqrt - calculates the square root.
Syntax
 { $sqrt: <number> } 

Simple examples:
 { $sqrt: 25 } 5 { $sqrt: 30 } 5.477225575051661 { $sqrt: null } null 

There is a collection of points with documents:
 { _id: 1, p1: { x: 5, y: 8 }, p2: { x: 0, y: 5} } { _id: 2, p1: { x: -2, y: 1 }, p2: { x: 1, y: 5} } { _id: 3, p1: { x: 4, y: 4 }, p2: { x: 4, y: 0} } 

The following example uses $sqrt to calculate the distance between p1 and p2 :
 db.points.aggregate([ { $project: { distance: { $sqrt: { $add: [ { $pow: [ { $subtract: [ "$p2.y", "$p1.y" ] }, 2 ] }, { $pow: [ { $subtract: [ "$p2.x", "$p1.x" ] }, 2 ] } ] } } } } ]) 

The output is the following result:
 { "_id" : 1, "distance" : 5.830951894845301 } { "_id" : 2, "distance" : 5 } { "_id" : 3, "distance" : 4 } 

$abs - returns the absolute value of a number.
Syntax
 { $abs: <number> } 

A simple example:
 { $abs: -1 } 1 { $abs: 1 } 1 { $abs: null } null 


The ratings collection has documents:
 { _id: 1, start: 5, end: 8 } { _id: 2, start: 4, end: 4 } { _id: 3, start: 9, end: 7 } { _id: 4, start: 6, end: 7 } 

The following example calculates the difference between the initial and final ratings:
 db.ratings.aggregate([ { $project: { delta: { $abs: { $subtract: [ "$start", "$end" ] } } } } ]) 

At the exit:
 { "_id" : 1, "delta" : 3 } { "_id" : 2, "delta" : 0 } { "_id" : 3, "delta" : 2 } { "_id" : 4, "delta" : 1 } 


$log - calculates the logarithm.
Syntax
 { $log: [ <number>, <base> ] } 

Calculates the logarithm of a number, and returns the result as a fraction.
There is a collection of examples :
 { _id: 1, positiveInt: 5 } { _id: 2, positiveInt: 2 } { _id: 3, positiveInt: 23 } { _id: 4, positiveInt: 10 } 

The following example uses log2 to determine the number of bits required to represent a positiveInt value.
 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2 , WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



 . 
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



.
:
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }

$ln - .

{ $ln: <number> }
{ _id: 1, year: "2000", sales: 8700000 } { _id: 2, year: "2005", sales: 5000000 } { _id: 3, year: "2010", sales: 6250000 }
, :
db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )
:
{ "_id" : 1, "x" : "2000", "y" : 15.978833583624812 } { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 } { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

$pow - ( ).
:
{ $pow: [ <number>, <exponent> ] } :
{ $pow: [ 5, 0 ] } 1 { $pow: [ 5, 2 ] } 25 { $pow: [ 5, -2 ] } 0.04
{ "_id" : 1, "scores" : [ { "name" : "dave123", "score" : 85 }, { "name" : "dave2", "score" : 90 }, { "name" : "ahn", "score" : 71 } ] } { "_id" : 2, "scores" : [ { "name" : "li", "quiz" : 2, "score" : 96 }, { "name" : "annT", "score" : 77 }, { "name" : "ty", "score" : 82 } ] }
, :
db.quizzes.aggregate([ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ])
:
{ "_id" : 1, "variance" : 64.66666666666667 } { "_id" : 2, "variance" : 64.66666666666667 }

$exp - .

{ exp: <number> }
:
{ $exp: 0 } 1 { $exp: 2 } 7.38905609893065 { $exp: -2 } 0.1353352832366127

$trunc - .
:
{ $trunc: <number> }
{ $trunc: 0 } 0 { $trunc: 7.80 } 7 { $trunc: -2.3 } -2

$ceil - , .
:
{ $ceil: <number> }
{ $ceil: 1 } 1 { $ceil: 7.80 } 8 { $ceil: -2.8 } -2

$floor - , .
:
{ floor: <number> }
:
{ $floor: 1 } 1 { $floor: 7.80 } 7 { $floor: -2.8 } -3

{ _id: 1, value: 9.25 } { _id: 2, value: 8.73 } { _id: 3, value: 4.32 } { _id: 4, value: -5.34 } db.samples.aggregate([ { $project: { value: 1, floorValue: { $floor: "$value" } } } ]) { "_id" : 1, "value" : 9.25, "floorValue" : 9 } { "_id" : 2, "value" : 8.73, "floorValue" : 8 } { "_id" : 3, "value" : 4.32, "floorValue" : 4 } { "_id" : 4, "value" : -5.34, "floorValue" : -6 }


$slice - .
:
{ $slice: [ <array>, <n> ] } { $slice: [ <array>, <position>, <n> ] }
{ "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
{ "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
{ "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
{ "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }
, favorites :
db.users.aggregate([ { $project: { name: 1, threeFavorites: { $slice: [ "$favorites", 3 ] } } } ]) { "_id" : 1, "name" : "dave123", "threeFavorites" : [ "chocolate", "cake", "butter" ] } { "_id" : 2, "name" : "li", "threeFavorites" : [ "apples", "pudding", "pie" ] } { "_id" : 3, "name" : "ahn", "threeFavorites" : [ "pears", "pecans", "chocolate" ] } { "_id" : 4, "name" : "ty", "threeFavorites" : [ "ice cream" ] }

$arrayElemAt - .
:
{ $arrayElemAt: [ <array>, <idx> ] }
:
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1 { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2 { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

$concatArrays - .
:
{ $concatArrays: [ <array1>, <array2>, ... ] }
:
{ $concatArrays: [ [ "hello", " "], [ "world" ] ] } [ "hello", " ", "world" ] { $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"] ] } [ "hello", " ", [ "world" ], "again" ]

$isArray - , .

{ $isArray: [ ] }
warehouses :
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] } { "_id" : 2, instock: [ "apples", "pudding", "pie" ] } { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] } { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }

, instock , ordered , , , , :

db.warehouses.aggregate([ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ]) { "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }

$filter - .

{ $filter: { input: <array>, as: <string>, cond: <expression> } }
:
{ $filter: { input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ], as: "num", cond: { $and: [ { $gte: [ "$$num", NumberLong("-9223372036854775807") ] }, { $lte: [ "$$num", NumberLong("9223372036854775807") ] } ] } } } [ 1, 2, 3.1, NumberLong(4) ]

$project
mongodb 3.2 , $project , [] , .
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "x" : 1, "y" : 1 }
$project x y myArray :
db.collection.aggregate( [ { $project: { myArray: [ "$x", "$y" ] } } ] )
:
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1 ] }
,
mongorestore mongodump , , , .

test.20150715.archive test . .
mongorestore --archive=test.20150715.archive --db test
, mongorestore archive . :
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018

CRUD

CRUD (Create/Read/Update/Delete) API , mongo shell .

db.collection.bulkWrite() - , .
, bulkWrite() , :
db.collection.bulkWrite( [ { insertOne : <document> }, { updateOne : <document> }, { updateMany : <document> }, { replaceOne : <document> }, { deleteOne : <document> }, { deleteMany : <document> } ], { ordered : false } )
ordered : false , . , deleteOne deleteMany insertOne , updateOne , updateMany replaceOne .

db.collection.deleteMany() - db.collection.remove() .
db.collection.deleteOne() - db.collection.remove() justOne true
- db.collection.remove( , true ) db.collection.remove( , { justOne: true } ).
db.collection.findOneAndDelete()
- db.collection.findAndModify() remove true.
db.collection.findOneAndReplace() - db.collection.findAndModify() .
db.collection.findOneAndUpdate() - db.collection.findAndModify() update .
db.collection.insertMany() - db.collection.insert() .
db.collection.insertOne() - db.collection.insert() .
db.collection.replaceOne() - db.collection.update( , ) .
db.collection.updateMany()
- db.collection.update( , , { multi: true, ... }) multi true .
db.collection.updateOne() - db.collection.update( , )

WiredTiger fsyncLock
MongoDB 3.2
, WiredTiger fsync lock mongo shell db.fsyncLock(). , WiredTiger , , , .
WiredTiger .

GUI compass
Mongodb , , GUI . MongoDB Compass . compass c web . nosql couchdb futon .

compass, , Mac OS Windows. , , .

, GUI Windows mongoDB , :
, β€œβ€ C:\mongodb .
:
mkdir c:\data\db mkdir c:\data\log
C:\mongodb\mongod.cfg :
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
mongodb :
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
:
net start MongoDB
, . , c:\mongodb\mongo.exe

compass, . :

, , . , .

, , (, ) json.


, , GUI , - .

PS .

mongodb windows
compass
mongodb compass



Source: https://habr.com/ru/post/192870/


All Articles