Mongodb 3.2 рдХреБрдЫ рд╡рд┐рд╡рд░рдг рдЬрд╛рд░реА рдХрд░рддрд╛ рд╣реИ


рджреВрд╕рд░реЗ рджрд┐рди, mongodb рдХреА рдПрдХ рдирдИ рд╕реНрдерд┐рд░ рд░рд┐рд▓реАрдЬ рдЬрд╛рд░реА рдХреА mongodb ред рдЗрд╕ рд╕рдВрд╕реНрдХрд░рдг рдореЗрдВ, рдХрдИ рдирд╡рд╛рдЪрд╛рд░реЛрдВ рдХреЛ рдЬреЛрдбрд╝рд╛ рдЧрдпрд╛ рдЬреИрд╕реЗ рдХрд┐ mongodb , LEFT JOIN , рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рд╕рддреНрдпрд╛рдкрди, рдЖрджрд┐ рдХреЗ рд╕рд╛рде рджреГрд╢реНрдп рдХрд╛рд░реНрдп рдХреЗ рд▓рд┐рдП рдПрдХ рдирдпрд╛ GUI ред рд╣рдо рдиреАрдЪреЗ рджрд┐рдП рдЧрдП рдЫреЛрдЯреЗ рдЙрджрд╛рд╣рд░рдгреЛрдВ рдореЗрдВ рдЗрдирдореЗрдВ рд╕реЗ рдХреБрдЫ рдЧреБрдгреЛрдВ рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВрдЧреЗред


рдЖрдВрд╢рд┐рдХ рд╕реВрдЪрдХрд╛рдВрдХ


MongoDB 3.2 рдХреЗрд╡рд▓ рдЙрди рд╕рдВрдЧреНрд░рд╣ рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЗ рд▓рд┐рдП рдЕрдиреБрдХреНрд░рдорд┐рдд рдмрдирд╛рдиреЗ рдХреА рдХреНрд╖рдорддрд╛ рдкреНрд░рджрд╛рди рдХрд░рддрд╛ рд╣реИ рдЬреЛ рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдлрд╝рд┐рд▓реНрдЯрд░ рд╕реЗ рдореЗрд▓ рдЦрд╛рдПрдВрдЧреЗред рд╕реВрдЪрдХрд╛рдВрдХ рдореЗрдВ рдЖрд╡рд╢реНрдпрдХ рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЗ рдХреЗрд╡рд▓ рдПрдХ рд╣рд┐рд╕реНрд╕реЗ рдХреЛ рдЕрдиреБрдХреНрд░рдордгрд┐рдд рдХрд░рдиреЗ рд╕реЗ рдХрдИ рдлрд╛рдпрджреЗ рд╣реЛрддреЗ рд╣реИрдВ, рдЬреИрд╕реЗ рдХрд┐ рдХрдо рднрдВрдбрд╛рд░рдг рдЖрд╡рд╢реНрдпрдХрддрд╛рдПрдВ, рдФрд░ рдХрдо рдЙрддреНрдкрд╛рджрдХрддрд╛ рд▓рд╛рдЧрдд, рдПрдХ рд╕реВрдЪрдХрд╛рдВрдХ рдмрдирд╛рдиреЗ рдФрд░ рдмрдирд╛рдП рд░рдЦрдиреЗ рдХреЗ рд▓рд┐рдПред рдЖрдк рд╕рднреА рдкреНрд░рдХрд╛рд░ рдХреЗ MongoDB рдЗрдВрдбреЗрдХреНрд╕ рдХреЗ рд▓рд┐рдП partialFilterExpression рд╡рд┐рдХрд▓реНрдк рдХреЛ рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред

рдПрдХ рдЖрдВрд╢рд┐рдХ рдЕрдиреБрдХреНрд░рдордгрд┐рдХрд╛ рдмрдирд╛рдиреЗ рдХреЗ рд▓рд┐рдП, рдирдП partialFilterExpression рд╡рд┐рдХрд▓реНрдк рдХреЗ рд╕рд╛рде db.collection.createIndex() рд╡рд┐рдзрд┐ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░реЗрдВред рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдЙрджрд╛рд╣рд░рдг рдореЗрдВ, рдПрдХ рд╕рдордЧреНрд░ рд╕реВрдЪрдХрд╛рдВрдХ рдмрдирд╛рдПрдВ рдЬреЛ рдХреЗрд╡рд▓ рдЙрди рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЛ 5 рд╕реЗ рдЕрдзрд┐рдХ рдХреА рд░реЗрдЯрд┐рдВрдЧ рдХреЗ рд╕рд╛рде рдЕрдиреБрдХреНрд░рдорд┐рдд рдХрд░реЗрдЧрд╛ред
 db.restaurants.createIndex( { cuisine: 1, name: 1 }, { partialFilterExpression: { rating: { $gt: 5 } } } ) 

рд╡рд┐рд░рд▓ рд╕реВрдЪрдХрд╛рдВрдХ рдХреЗ рд╕рд╛рде рддреБрд▓рдирд╛ред


рдПрдХ рдЖрдВрд╢рд┐рдХ рд╕реВрдЪрдХрд╛рдВрдХ рдПрдХ рд╡рд┐рд░рд▓ рд╕реВрдЪрдХрд╛рдВрдХ рдХреА рддреБрд▓рдирд╛ рдореЗрдВ рдХреНрд╖рдорддрд╛рдУрдВ рдХрд╛ рдПрдХ рдмрдбрд╝рд╛ рд╕реЗрдЯ рдкреНрд░рджрд╛рди рдХрд░рддрд╛ рд╣реИ рдФрд░ рдЗрд╕рд▓рд┐рдП рдпрд╣ рдмреЗрд╣рддрд░ рд╣реИред рдПрдХ рдЖрдВрд╢рд┐рдХ рд╕реВрдЪрдХрд╛рдВрдХ рдпрд╣ рдЗрдВрдЧрд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдЕрдзрд┐рдХ рдЕрднрд┐рд╡реНрдпрдВрдЬрдХ рддрдВрддреНрд░ рдкреНрд░рджрд╛рди рдХрд░рддрд╛ рд╣реИ рдХрд┐ рдХреМрди рд╕реЗ рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рд╕рд░реНрд╡реЛрддреНрддрдо рдЕрдиреБрдХреНрд░рдорд┐рдд рд╣реИрдВред
рд╕реНрдкрд╛рд░реНрд╕ рдЕрдиреБрдХреНрд░рдорд┐рдд рдЕрдиреБрдХреНрд░рдорд┐рдд рдХреНрд╖реЗрддреНрд░ рдХреА рдЙрдкрд╕реНрдерд┐рддрд┐ рдХреЗ рдЖрдзрд╛рд░ рдкрд░ рдХреЗрд╡рд▓ рдЕрдиреБрдХреНрд░рдордг рдХреЗ рд▓рд┐рдП рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХрд╛ рдЪрдпрди рдХрд░рддреЗ рд╣реИрдВред
рдЖрдВрд╢рд┐рдХ рд╕реВрдЪрдХрд╛рдВрдХ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рддреЗ рд╣реБрдП рд╡рд┐рд░рд▓ рд╕реВрдЪрдХрд╛рдВрдХ рд╡реНрдпрд╡рд╣рд╛рд░ рдХрд╛ рдПрдХ рдЙрджрд╛рд╣рд░рдг:
 db.contacts.createIndex( { name: 1 }, { partialFilterExpression: { name: { $exists: true } } } ) 


рдПрдХ рдЖрдВрд╢рд┐рдХ рд╕реВрдЪрдХрд╛рдВрдХ рдХреЗ рд╕рд╛рде рдПрдХ рдФрд░ рдЙрджрд╛рд╣рд░рдг рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВ, рдпрд╣рд╛рдВ рдЙрд╕ рдХреНрд╖реЗрддреНрд░ рдХреЛ рдХрд┐рд╕ name рд╕реЗ рдЕрдиреБрдХреНрд░рдорд┐рдд рдХрд┐рдпрд╛ рдЬрд╛рдПрдЧрд╛, рдФрд░ рд╡рд╣ рдХреНрд╖реЗрддреНрд░ рдЬрд┐рд╕рдХреЗ рджреНрд╡рд╛рд░рд╛ рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЛ рдлрд╝рд┐рд▓реНрдЯрд░ рдХрд┐рдпрд╛ рдЬрд╛рдПрдЧрд╛ рд╡рд╣ рд╣реИ:
 db.contacts.createIndex( { name: 1 }, { partialFilterExpression: { email: { $exists: true } } } ) 

рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдХреНрд╡реЗрд░реА рдПрдХ рд╕реВрдЪрдХрд╛рдВрдХ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░ рд╕рдХрддреА рд╣реИ:
 db.contacts.find( { name: "xyz", email: { $regex: /\.org$/ } } ) 

рдФрд░ рдЗрд╕ рдХреНрд╡реЗрд░реА рдХреЗ рд▓рд┐рдП, рдЗрдВрдбреЗрдХреНрд╕ рдХрд╛ рдЙрдкрдпреЛрдЧ рдирд╣реАрдВ рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИ:
 db.contacts.find( { name: "xyz", email: { $exists: false } } ) 

рдкреНрд░рддрд┐рдмрдВрдз


mongodb рдЖрдк рдПрдХ рдЗрдВрдбреЗрдХреНрд╕ рдХреЗ рдХрдИ рд╕рдВрд╕реНрдХрд░рдг рдирд╣реАрдВ рдмрдирд╛ рд╕рдХрддреЗ рд╣реИрдВ рдЬреЛ рдХреЗрд╡рд▓ рдорд╛рдкрджрдВрдбреЛрдВ рдореЗрдВ рднрд┐рдиреНрди рд╣реЛрддреЗ рд╣реИрдВред рдЖрдк рдХрдИ рдЖрдВрд╢рд┐рдХ рдЕрдиреБрдХреНрд░рдордгрд┐рдХрд╛рдПрдБ рдирд╣реАрдВ рдмрдирд╛ рд╕рдХрддреЗ рдЬреЛ рдХреЗрд╡рд▓ рдлрд╝рд┐рд▓реНрдЯрд░ рдорд╛рдкрджрдВрдбреЛрдВ рдореЗрдВ рднрд┐рдиреНрди рд╣реЛрддреА рд╣реИрдВред
mongodb рдкрд╣рд▓реЗ рдХреЗ рд╕рдВрд╕реНрдХрд░рдгреЛрдВ рдиреЗ рдЖрдВрд╢рд┐рдХ рд╕реВрдЪрдХрд╛рдВрдХ рдХрд╛ рд╕рдорд░реНрдерди рдирд╣реАрдВ рдХрд┐рдпрд╛ред рд╕рднреА sharded рдХреНрд▓рд╕реНрдЯрд░, рдкреНрд░рддрд┐рдХреГрддрд┐ рд╕реЗрдЯ, рдФрд░ рд╕рднреА рдиреЛрдбреНрд╕ mongodb 3.2 рдкрд░ рдХрд╛рдо рдХрд░рдирд╛ рдЪрд╛рд╣рд┐рдПред
_id рдЗрдВрдбреЗрдХреНрд╕ рдЖрдВрд╢рд┐рдХ рдЗрдВрдбреЗрдХреНрд╕ рдирд╣реАрдВ рд╣реЛ рд╕рдХрддрд╛ред
рдПрдХ рд╢рд╛рд░реНрдХ рдХреБрдВрдЬреА рдПрдХ рдЖрдВрд╢рд┐рдХ рд╕реВрдЪрдХрд╛рдВрдХ рдирд╣реАрдВ рд╣реЛ рд╕рдХрддреАред

restaurants рд╕рдВрдЧреНрд░рд╣ рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВ, рдЬрд┐рд╕рдореЗрдВ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рджрд╕реНрддрд╛рд╡реЗрдЬ рд╢рд╛рдорд┐рд▓ рд╣реИрдВ рдФрд░ рдЗрд╕ рддрд░рд╣ рджрд┐рдЦрддрд╛ рд╣реИ:
 { "_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" } 

рд╣рдо рдЙрди рджрд╕реНрддрд╛рд╡реЗрдЬрд╝реЛрдВ рдХреЛ рдХреЗрд╡рд▓ рдлрд╝рд┐рд▓реНрдЯрд░ cuisine рдЙрди рдХреНрд╖реЗрддреНрд░реЛрдВ рдХреЗ рд▓рд┐рдП рдПрдХ рдЗрдВрдбреЗрдХреНрд╕ рдЬреЛрдбрд╝ рд╕рдХрддреЗ рд╣реИрдВ рдЬреЛ borough рдФрд░ cuisine
 db.restaurants.createIndex( { borough: 1, cuisine: 1 }, { partialFilterExpression: { 'rating.grade': { $eq: "A" } } } ) 

рдПрдХ рдЖрдВрд╢рд┐рдХ рдЕрдиреБрдХреНрд░рдордгрд┐рдХрд╛ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдХреЗ restaurants рд╕рдВрдЧреНрд░рд╣ рдкрд░ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдХреНрд╡реЗрд░реА Bronx рдореЗрдВ рд╕реНрдерд┐рдд рд╕рднреА рд░реЗрд╕реНрддрд░рд╛рдВ рд╡рд╛рдкрд╕ rating.grade рдЬрд╣рд╛рдВ rating.grade A рдмрд░рд╛рдмрд░ рд╣реИ
 db.restaurants.find( { borough: "Bronx", 'rating.grade': "A" } ) 

рдФрд░ рдпрд╣ рдЕрдиреБрд░реЛрдз рдЗрдВрдбреЗрдХреНрд╕ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдореЗрдВ рд╕рдХреНрд╖рдо рдирд╣реАрдВ рд╣реЛрдЧрд╛, рдХреНрдпреЛрдВрдХрд┐ rating.grade рдХреЛрдИ рдлрд╝реАрд▓реНрдб рдирд╣реАрдВ рд╣реИрдВ
 db.restaurants.find( { borough: "Bronx", cuisine: "Bakery" } ) 

рдЖрдВрд╢рд┐рдХ рд╡рд┐рд╢рд┐рд╖реНрдЯрддрд╛ рдкреНрд░рддрд┐рдмрдВрдзрд┐рдд рд╕реВрдЪрдХрд╛рдВрдХ


рдПрдХ рдЖрдВрд╢рд┐рдХ рд╕реВрдЪрдХрд╛рдВрдХ рдХреЛ рдЙрди рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдкрд░ рд▓рд╛рдЧреВ рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИ рдЬрд┐рдирдореЗрдВ рдПрдХ рдЕрджреНрд╡рд┐рддреАрдп рдмрд╛рдзрд╛ рд╣реЛрддреА рд╣реИ ред

рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рд╡рд╛рд▓реЗ users рд╕рдВрдЧреНрд░рд╣ рдХреЛ рд▓реЗрдВ:
 { "_id" : ObjectId("56424f1efa0358a27fa1f99a"), "username" : "david", "age" : 29 } { "_id" : ObjectId("56424f37fa0358a27fa1f99b"), "username" : "amanda", "age" : 35 } { "_id" : ObjectId("56424fe2fa0358a27fa1f99c"), "username" : "rajiv", "age" : 57 } 

рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдСрдкрд░реЗрд╢рди рдПрдХ рд╕реВрдЪрдХрд╛рдВрдХ рдмрдирд╛рдПрдЧрд╛ рдЬреЛ username рдлрд╝реАрд▓реНрдб рдкрд░ рдПрдХ рдЕрджреНрд╡рд┐рддреАрдп рдмрд╛рдзрд╛ рдФрд░ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдХреА age: { $gte: 21 } рд▓рд┐рдП рдПрдХ рдЖрдВрд╢рд┐рдХ рдлрд┐рд▓реНрдЯрд░ рд▓рдЧрд╛рддрд╛ рд╣реИ age: { $gte: 21 }
 db.users.createIndex( { username: 1 }, { unique: true, partialFilterExpression: { age: { $gte: 21 } } } ) 

рд╕реВрдЪрдХрд╛рдВрдХ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЗ рд╕рдореНрдорд┐рд▓рди рдХреЛ рд░реЛрдХрддрд╛ рд╣реИ рдХреНрдпреЛрдВрдХрд┐ рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдирд╛рдо рд╡рд╛рд▓реЗ рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рдкрд╣рд▓реЗ рд╕реЗ рдореМрдЬреВрдж рд╣реИрдВ рдФрд░ age рдХреНрд╖реЗрддреНрд░ 21 рд╕реЗ рдЕрдзрд┐рдХ рд╣реИ:
 db.users.insert( { username: "david", age: 27 } ) db.users.insert( { username: "amanda", age: 25 } ) db.users.insert( { username: "rajiv", age: 32 } ) 

рдФрд░ рдпреЗ рджрд╕реНрддрд╛рд╡реЗрдЬ рдбрд╛рд▓реЗ рдЬрд╛рдПрдВрдЧреЗ:
 db.users.insert( { username: "david", age: 20 } ) db.users.insert( { username: "amanda" } ) db.users.insert( { username: "rajiv", age: null } ) 


рдорд╛рдиреНрдпрддрд╛


рд╕рдВрд╕реНрдХрд░рдг 3.2 рд╢реБрд░реВ рд╣реЛрдХрд░, рдореЛрдВрдЧреЛрдбрдм рдЕрджреНрдпрддрди рдФрд░ рд╕рдореНрдорд┐рд▓рди рдкреНрд░рдХреНрд░рд┐рдпрд╛ рдХреЗ рджреМрд░рд╛рди рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреА рдЬрд╛рдВрдЪ рдХрд░рдиреЗ рдХреА рдХреНрд╖рдорддрд╛ рдкреНрд░рджрд╛рди рдХрд░рддрд╛ рд╣реИред рд╕рддреНрдпрд╛рдкрди рдирд┐рдпрдо рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдХрд┐рдП рдЬрд╛рддреЗ рд╣реИрдВ, рдЬрдм рд╕рдВрдЧреНрд░рд╣ рд╡реИрдз рд░реВрдк рд╕реЗ рд╡рд┐рдХрд▓реНрдк рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдХреЗ, рдпрд╛ рдХрд┐рд╕реА рдореМрдЬреВрджрд╛ рд╕рдВрдЧреНрд░рд╣ рдХреЗ рд▓рд┐рдП collMod рдХрдорд╛рдВрдб рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдХреЗ рдмрдирд╛рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред validator рд╡рд┐рдХрд▓реНрдк рдПрдХ рдРрд╕реЗ рджрд╕реНрддрд╛рд╡реЗрдЬ рдХреЛ рд╕реНрд╡реАрдХрд╛рд░ рдХрд░рддрд╛ рд╣реИ рдЬреЛ рдХреБрдЫ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐рдпреЛрдВ рд╕реЗ рдореЗрд▓ рдЦрд╛рддрд╛ рд╣реИ, $geoNear рдХреЗ рдЕрдкрд╡рд╛рдж рдХреЗ рд╕рд╛рде, $near , $nearSphere $near , $text рдФрд░ $where ред

contacts рд╕рдВрдЧреНрд░рд╣ рдмрдирд╛рддреЗ рд╕рдордп рд╕рддреНрдпрд╛рдкрди рдмрдирд╛рдиреЗ рдХреЗ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдЙрджрд╛рд╣рд░рдг рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВ:
 db.createCollection( "contacts", { validator: { $or: [ { phone: { $type: "string" } }, { email: { $regex: /@mongodb\.com$/ } }, { status: { $in: [ "Unknown", "Incomplete" ] } } ] } } ) 

MongoDB рдПрдХ validationLevel рд╡рд┐рдХрд▓реНрдк рднреА рдкреНрд░рджрд╛рди рдХрд░рддрд╛ рд╣реИ, рдЬреЛ рдирд┐рд░реНрдзрд╛рд░рд┐рдд рдХрд░рддрд╛ рд╣реИ рдХрд┐ MongoDB рдореМрдЬреВрджрд╛ рджрд╕реНрддрд╛рд╡реЗрдЬрд╝реЛрдВ рдХреЗ рд╕рддреНрдпрд╛рдкрди рдХреЗ рдирд┐рдпрдореЛрдВ рдХреЛ рдХрд┐рддрдиреА рд╕рдЦреНрддреА рд╕реЗ рд▓рд╛рдЧреВ рдХрд░рддрд╛ рд╣реИ рдЬрдм рд╡реЗ рдЕрдкрдбреЗрдЯ рдХрд┐рдП рдЬрд╛рддреЗ рд╣реИрдВ, рдФрд░ рдПрдХ validationAction рд╡рд┐рдХрд▓реНрдк, рдЬреЛ рдпрд╣ рдирд┐рд░реНрдзрд╛рд░рд┐рдд рдХрд░рддрд╛ рд╣реИ рдХрд┐ рдХреНрдпрд╛ рдореЛрдВрдбреЛрдб рдХреЛ рд╕рддреНрдпрд╛рдкрди рдирд┐рдпрдореЛрдВ рдХрд╛ рдЙрд▓реНрд▓рдВрдШрди рдХрд░рдиреЗ рд╡рд╛рд▓реЗ рджрд╕реНрддрд╛рд╡реЗрдЬрд╝реЛрдВ рдХреЛ рдЕрд╕реНрд╡реАрдХрд╛рд░ рдХрд░рдирд╛ рдЪрд╛рд╣рд┐рдПред

рд╡реНрдпрд╡рд╣рд╛рд░


рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЛ рдЕрдкрдбреЗрдЯ рдФрд░ рд╕рдореНрдорд┐рд▓рд┐рдд рдХрд░рддреЗ рд╕рдордп рд╡реИрдзрддрд╛ рд╣реЛрддреА рд╣реИред рд╕рдВрдЧреНрд░рд╣ рдореЗрдВ рдорд╛рдиреНрдпрддрд╛ рдЬреЛрдбрд╝рддреЗ рд╕рдордп, рдореМрдЬреВрджрд╛ рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЛ рд╕рдВрд╢реЛрдзрд┐рдд рдирд╣реАрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

рдореМрдЬреВрджрд╛ рджрд╕реНрддрд╛рд╡реЗрдЬрд╝


рдЖрдк рдирд┐рдпрдВрддреНрд░рд┐рдд рдХрд░ рд╕рдХрддреЗ рд╣реИрдВ рдХрд┐ mongodb рд╡рд┐рдХрд▓реНрдк validationLevel рд╡рд┐рдХрд▓реНрдк рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдХреЗ рдореМрдЬреВрджрд╛ рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЛ рдХреИрд╕реЗ рд╕рдВрд╕рд╛рдзрд┐рдд рдХрд░рддрд╛ рд╣реИред
рдбрд┐рдлрд╝реЙрд▓реНрдЯ рд░реВрдк рд╕реЗ, validationLevel mongodb strict , рдФрд░ mongodb рд╕рднреА рд╕рдореНрдорд┐рд▓рди рдФрд░ рдЕрджреНрдпрддрдиреЛрдВ рдХреЗ рд▓рд┐рдП рд╕рддреНрдпрд╛рдкрди рдирд┐рдпрдо рд▓рд╛рдЧреВ рдХрд░рддрд╛ рд╣реИред рдпрджрд┐ validationLevel рдХреЛ moderate рд╕реЗрдЯ рдХрд┐рдпрд╛ рдЧрдпрд╛ рд╣реИ, рддреЛ рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЛ рд╕рдореНрдорд┐рд▓рд┐рдд рдФрд░ рдЕрджреНрдпрддрди рдХрд░рддреЗ рд╕рдордп рд╕рддреНрдпрд╛рдкрди рдирд┐рдпрдо рдХреЗрд╡рд▓ рдЙрди рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдкрд░ рд▓рд╛рдЧреВ рд╣реЛрддреЗ рд╣реИрдВ рдЬреЛ рдкреВрд░реА рддрд░рд╣ рд╕реЗ рд╡реИрдз рдорд╛рдирджрдВрдбреЛрдВ рдХреЛ рдкреВрд░рд╛ рдХрд░рддреЗ рд╣реИрдВред

contacts рд╕рдВрдЧреНрд░рд╣ рдореЗрдВ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВ:
 { "_id": "125876" "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" }, { "_id": "860000", "name": "Ivan", "city": "Vancouver" } 

contacts рд╕рдВрдЧреНрд░рд╣ рдореЗрдВ рд╕рддреНрдпрд╛рдкрди рдЬреЛрдбрд╝рдиреЗ рдХреЗ рд▓рд┐рдП рдХрдорд╛рдВрдб рдЪрд▓рд╛рдПрдБ:
 db.runCommand( { collMod: "contacts", validator: { $or: [ { phone: { $exists: true } }, { email: { $exists: true } } ] }, validationLevel: "moderate" } ) 

contacts рд╕рдВрдЧреНрд░рд╣ рдореЗрдВ рдЕрдм рдПрдХ рд╕рддреНрдпрд╛рдкрдирдХрд░реНрддрд╛ рд╣реИред рдФрд░ рдЕрдм рдпрджрд┐ рд╣рдо рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рдХреЛ {_id: 125876} mongodb {_id: 125876} рд╕рд╛рде рдЕрдкрдбреЗрдЯ рдХрд░рддреЗ рд╣реИрдВ, рддреЛ mongodb рд╕рддреНрдпрд╛рдкрди рдирд┐рдпрдореЛрдВ рдХреЛ рд▓рд╛рдЧреВ рдХрд░реЗрдЧрд╛, рдХреНрдпреЛрдВрдХрд┐ рдореМрдЬреВрджрд╛ рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рдорд╛рдирджрдВрдбреЛрдВ рдХреЛ рдкреВрд░рд╛ рдХрд░рддрд╛ рд╣реИред рдФрд░ рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рдХреЗ рд▓рд┐рдП { _id:860000 } , рдореЛрдВрдЧреЙрдбрдм рдЕрдкрдбреЗрдЯ рдХреЗ рд▓рд┐рдП рд╕рддреНрдпрд╛рдкрди рдирд┐рдпрдореЛрдВ рдХреЛ рд▓рд╛рдЧреВ рдирд╣реАрдВ рдХрд░реЗрдЧрд╛, рдХреНрдпреЛрдВрдХрд┐ рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рд╕рддреНрдпрд╛рдкрди рдирд┐рдпрдо рдХрд╛ рдкрд╛рд▓рди рдирд╣реАрдВ рдХрд░рддрд╛ рд╣реИред
рдЕрдЧрд░ рд╣рдореЗрдВ рд╕рддреНрдпрд╛рдкрди рдХреЛ рдЕрдХреНрд╖рдо рдХрд░рдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реИ, рддреЛ рд╣рдо validationLevel off рдХрд░рдиреЗ рдХреЗ рд╡рд┐рдХрд▓реНрдк рдХреЛ рд╕реЗрдЯ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред

рдЕрд╕реНрд╡реАрдХрд╛рд░ рдХрд┐рдП рдЧрдП рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЛ рд╕реНрд╡реАрдХрд╛рд░ рдпрд╛ рдЕрд╕реНрд╡реАрдХрд╛рд░ рдХрд░реЗрдВ

validationAction рд╡рд┐рдХрд▓реНрдк рдпрд╣ рдирд┐рд░реНрдзрд╛рд░рд┐рдд рдХрд░рддрд╛ рд╣реИ рдХрд┐ рд╕рддреНрдпрд╛рдкрди рдирд┐рдпрдореЛрдВ рдХрд╛ рдЙрд▓реНрд▓рдВрдШрди рдХрд░рдиреЗ рд╡рд╛рд▓реЗ рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рд╕реЗ рдХреИрд╕реЗ рдирд┐рдкрдЯреЗрдВред
рдбрд┐рдлрд╝реЙрд▓реНрдЯ рд░реВрдк рд╕реЗ, validationAction error рд▓рд┐рдП рд╕реЗрдЯ рд╣реИ рдФрд░ MongoDB рд╕рддреНрдпрд╛рдкрди рдирд┐рдпрдо рдХрд╛ рдЙрд▓реНрд▓рдВрдШрди рд╣реЛрдиреЗ рдкрд░ рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЗ рдХрд┐рд╕реА рднреА рдЖрд╡реЗрд╖рдг рдФрд░ рдЕрдкрдбреЗрдЯ рдХреЛ рдЕрд╕реНрд╡реАрдХрд╛рд░ рдХрд░рддрд╛ рд╣реИред рдЬрдм validationAction рд▓рд┐рдП warn , рддреЛ MongoDB рджрд╕реНрддрд╛рд╡реЗрдЬрд╝реЛрдВ рдХреЛ рд▓реЙрдЧ рдХрд░рддрд╛ рд╣реИ, рд▓реЗрдХрд┐рди рдЖрд╡реЗрд╖рдг рдФрд░ рдЕрдкрдбреЗрдЯ рдХреА рдЕрдиреБрдорддрд┐ рджреЗрддрд╛ рд╣реИред

рдирд┐рдореНрди рдЙрджрд╛рд╣рд░рдг рдПрдХ рд╕рддреНрдпрд╛рдкрдирдХрд░реНрддрд╛ рдХреЗ рд╕рд╛рде рдПрдХ contacts рд╕рдВрдЧреНрд░рд╣ рдмрдирд╛рддрд╛ рд╣реИ рдЬреЛ рдпрд╣ рджрд░реНрд╢рд╛рддрд╛ рд╣реИ рдХрд┐ рд╕рдореНрдорд┐рд▓рд┐рдд рдпрд╛ рдЕрдкрдбреЗрдЯ рдХрд┐рдП рдЧрдП рджрд╕реНрддрд╛рд╡реЗрдЬрд╝реЛрдВ рдХреЛ рдирд┐рдореНрди рддреАрди рд╕реНрдерд┐рддрд┐рдпреЛрдВ рдореЗрдВ рд╕реЗ рдХрдо рд╕реЗ рдХрдо рдПрдХ рдХреЛ рдкреВрд░рд╛ рдХрд░рдирд╛ рд╣реЛрдЧрд╛:
  • phone рдлрд╝реАрд▓реНрдб рдПрдХ рд╕реНрдЯреНрд░рд┐рдВрдЧ рд╣реЛрдирд╛ рдЪрд╛рд╣рд┐рдП
  • email рдлрд╝реАрд▓реНрдб рдХреЛ рдЕрдиреБрдкрд╛рд▓рди рдХреЗ рд▓рд┐рдП рдирд┐рдпрдорд┐рдд рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рджреНрд╡рд╛рд░рд╛ рдЬрд╛рдБрдЪ рдХреА рдЬрд╛рдиреА рдЪрд╛рд╣рд┐рдП
  • status рдлрд╝реАрд▓реНрдб рдпрд╛ рддреЛ рдЕрдЬреНрдЮрд╛рдд рдпрд╛ рдЕрдкреВрд░реНрдг рд╣реЛрдирд╛ рдЪрд╛рд╣рд┐рдПред

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

рдЕрдЧрд▓реА рдкреНрд░рд╡рд┐рд╖реНрдЯрд┐ рд╡рд┐рдлрд▓ рд╣реЛрдиреА рдЪрд╛рд╣рд┐рдП, рд▓реЗрдХрд┐рди рдЪреВрдВрдХрд┐ рд╕рддреНрдпрд╛рдкрди рд╕рддреНрдпрд╛рдкрди 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" }
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" }

рдкреНрд░рддрд┐рдмрдВрдз


рдЖрдк admin , local рд╕рдВрдЧреНрд░рд╣реЛрдВ рдХреЛ рдорд╛рдиреНрдпрддрд╛ рдкреНрд░рджрд╛рди рдирд╣реАрдВ рдХрд░ рд╕рдХрддреЗред
рдФрд░ рдЖрдк system.* рд╕рдВрдЧреНрд░рд╣ рдХреЗ рд▓рд┐рдП рдПрдХ рд╕рддреНрдпрд╛рдкрдирдХрд░реНрддрд╛ рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдирд╣реАрдВ рдХрд░ рд╕рдХрддреЗ system.*

рдПрдЧреНрд░реАрдЧреЗрд╢рди рдлреНрд░реЗрдорд╡рд░реНрдХ рдФрд░ рд▓реЗрдлреНрдЯрд┐рдиреЗрдВрдЯ рдЗрдиреЛрд╡реЗрд╢рди


$lookup - рдХрдИ рд╕рдВрдЧреНрд░рд╣ рдХреЛ рд▓рд┐рдВрдХ рдХрд░рдирд╛ред
рдпрджреНрдпрдкрд┐ рдЗрд╕ рдСрдкрд░реЗрдЯрд░ рдХреЛ mongodb рд▓рд┐рдП рдЗрд╕рдХреЗ left join рдХрд╣рд╛ рдЬрд╛рддрд╛ рдерд╛ mongodb рдлрд┐рд░ рднреА рдпрд╣ рдореБрдЭреЗ рд▓рдЧрддрд╛ рд╣реИ рдХрд┐ рдпрд╣ рдкреНрд░рд╢реНрди рдереЛрдбрд╝рд╛ рд╡реНрдпрд╛рдкрдХ рд╣реИ, рдФрд░ рдЗрд╕реЗ рд╡рд┐рднрд┐рдиреНрди рдкреНрд░рдХрд╛рд░ рдХреЗ рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЛ рдЬреЛрдбрд╝рдиреЗ рдХреЗ рд╕рдВрджрд░реНрдн рдореЗрдВ рдорд╛рдирд╛ рдЬрд╛рдирд╛ рдЪрд╛рд╣рд┐рдП, рди рдХрд┐ рд╕рдВрдЧреНрд░рд╣ред рдореИрдВрдиреЗ рдЗрд╕ рдкреНрд░рд╢реНрди рдХрд╛ рдХреБрдЫ рд╡рд┐рд╕реНрддрд╛рд░ рд╕реЗ рдпрд╣рд╛рдБ рд╡рд░реНрдгрди рдХрд┐рдпрд╛ рд╣реИ ред
$lookup рдореЗрдВ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рд╕рд┐рдВрдЯреИрдХреНрд╕ рд╣реИ:
 { $lookup: { from: <  >, localField: <   >, foreignField: <     >, as: < > } } 

рдЙрджрд╛рд╣рд░рдг рдореЗрдВ, рд╣рдорд╛рд░реЗ рдкрд╛рд╕ рджреЛ рд╕рдВрдЧреНрд░рд╣ рд╣реИрдВ:
orders рд╕рдВрдЧреНрд░рд╣ рдореЗрдВ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рджрд╕реНрддрд╛рд╡реЗрдЬ рд╢рд╛рдорд┐рд▓ рд╣реИрдВ:
 { "_id" : 1, "item" : "abc", "price" : 12, "quantity" : 2 } { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1 } { "_id" : 3 } 

рдФрд░ рдПрдХ inventory рд╕рдВрдЧреНрд░рд╣ рдЬрд┐рд╕рдореЗрдВ рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рд╣реИрдВ:
 { "_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 } 

рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рд╕рдВрдЧреНрд░рд╣ рдСрдкрд░реЗрд╢рди, orders рдХрд▓реЗрдХреНрд╢рди рдХреЗ рд▓рд┐рдП, рдбреЙрдХреНрдпреБрдореЗрдВрдЯреНрд╕ рд╕реЗ orders рд╕рд╛рде рдбреЙрдХреНрдпреВрдореЗрдВрдЯ рдлреАрд▓реНрдб рд╕реЗ orders рдФрд░ inventory рд╕реЗ sku рдлреАрд▓реНрдб рд╕реЗ рдбреЙрдХреНрдпреБрдореЗрдВрдЯреНрд╕ рдХреЛ рд▓рд┐рдВрдХ рдХрд░реЗрдЧрд╛ред
 db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } } ]) 

рдмрд╛рд╣рд░ рдирд┐рдХрд▓рдиреЗ рдкрд░, рдпрд╣ рдСрдкрд░реЗрд╢рди рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рджрд╕реНрддрд╛рд╡реЗрдЬ рд▓реМрдЯрд╛рдПрдЧрд╛:
 { "_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 - рдмреЗрддрд░рддреАрдм рдврдВрдЧ рд╕реЗ рдЗрдирдкреБрдЯ рд╕реНрдЯреНрд░реАрдо рд╕реЗ рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреА рдПрдХ рд╕рдВрдЦреНрдпрд╛ рдХрд╛ рдЪрдпрди рдХрд░рддрд╛ рд╣реИред
рд╡рд╛рдХреНрдп рд░рдЪрдирд╛:
 { $sample: { size: <positive integer> } } 

рдЙрджрд╛рд╣рд░рдг рдореЗрдВ, рд╣рдорд╛рд░реЗ рдкрд╛рд╕ рдПрдХ users рд╕рдВрдЧреНрд░рд╣ рд╣реИ рдЬрд┐рд╕рдореЗрдВ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рджрд╕реНрддрд╛рд╡реЗрдЬ рд╣реИрдВ:
 { "_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 } 

рдПрдХрддреНрд░реАрдХрд░рдг рдСрдкрд░реЗрд╢рди рдмреЗрддрд░рддреАрдм рдврдВрдЧ рд╕реЗ рд╕рдВрдЧреНрд░рд╣ рд╕реЗ рддреАрди рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХрд╛ рдЪрдпрди рдХрд░реЗрдЧрд╛:
 db.users.aggregate( [ { $sample: { size: 3 } } ] ) 

PostgreSQL 9.5 , рдЬреЛ рд░рд┐рд▓реАрдЬрд╝ рд╣реЛрдиреЗ рд╡рд╛рд▓рд╛ рд╣реИ, рдореЗрдВ рднреА рдПрдХ рд╕рдорд╛рди рд╕реБрд╡рд┐рдзрд╛ рд╣реИред

$indexStats - рдкреНрд░рддреНрдпреЗрдХ рд╕реВрдЪрдХрд╛рдВрдХ рдХреЗ рд▓рд┐рдП рдЙрдкрдпреЛрдЧ рдХреЗ рдЖрдВрдХрдбрд╝реЗ рд▓реМрдЯрд╛рддрд╛ рд╣реИред
рдПрдХ рд╕рдВрдЧреНрд░рд╣ рдХреЗ рд▓рд┐рдП рдкреНрд░рддреНрдпреЗрдХ рд╕реВрдЪрдХрд╛рдВрдХ рдХреЗ рд▓рд┐рдП рдЙрдкрдпреЛрдЧ рдХреЗ рдЖрдВрдХрдбрд╝реЗ рд▓реМрдЯрд╛рддрд╛ рд╣реИред рдпрджрд┐ рдЕрднрд┐рдЧрдо рдирд┐рдпрдВрддреНрд░рдг рдХреЗ рд╕рд╛рде рд▓реЙрдиреНрдЪ рдХрд┐рдпрд╛ рдЧрдпрд╛ рд╣реИ, рддреЛ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдХреЗ рдкрд╛рд╕ рд╡рд┐рд╢реЗрд╖рд╛рдзрд┐рдХрд╛рд░реЛрдВ рдХреЛ рд╢рд╛рдорд┐рд▓ рдХрд░рдирд╛ рдЪрд╛рд╣рд┐рдПред

рд╡рд╛рдХреНрдп рд░рдЪрдирд╛:
 { $indexStats: { } } 

рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, orders рд╕рдВрдЧреНрд░рд╣ рдХреЛ рд▓реЗрдВ, рдЬрд┐рд╕рдореЗрдВ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рджрд╕реНрддрд╛рд╡реЗрдЬ рд╣реИрдВ:
 { "_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" } 

рд╕рдВрдЧреНрд░рд╣ рдкрд░ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рджреЛ рд╕реВрдЪрдХрд╛рдВрдХ рдмрдирд╛рдПрдБ:
 db.orders.createIndex( { item: 1, quantity: 1 } ) db.orders.createIndex( { type: 1, item: 1 } ) 

рдЖрдЗрдП рдХреБрдЫ рдкреНрд░рд╢реНрдиреЛрдВ рдкрд░ рдЕрдорд▓ рдХрд░реЗрдВ:
 db.orders.find( { type: "apparel"} ) db.orders.find( { item: "abc" } ).sort( { quantity: 1 } ) 

orders рд╕рдВрдЧреНрд░рд╣ рдХреЗ рд▓рд┐рдП рд╕реВрдЪрдХрд╛рдВрдХ рдХреЗ рдЙрдкрдпреЛрдЧ рдкрд░ рдЖрдВрдХрдбрд╝реЗ рджреЗрдЦрдиреЗ рдХреЗ рд▓рд┐рдП, рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдСрдкрд░реЗрд╢рди рдХрд░реЗрдВ:
 db.orders.aggregate( [ { $indexStats: { } } ] ) 

рдСрдкрд░реЗрд╢рди рдкреНрд░рддреНрдпреЗрдХ рд╕реВрдЪрдХрд╛рдВрдХ рдХреЗ рдЖрдБрдХрдбрд╝реЛрдВ рд╡рд╛рд▓реЗ рджрд╕реНрддрд╛рд╡реЗрдЬрд╝ рд▓реМрдЯрд╛рдПрдЧрд╛:
 { "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 рд╣реИред
$group рдФрд░ $project рд╕реЗ рдЙрдкрд▓рдмреНрдз, рдЧреИрд░-рд╕рдВрдЦреНрдпрд╛рддреНрдордХ рдорд╛рдиреЛрдВ рдХреЛ рдЕрдирджреЗрдЦрд╛ рдХрд░рддрд╛ рд╣реИ рдФрд░ рдпрджрд┐ рд╣рд╛рдВ, рддреЛ null ред
рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, users рд╕рдВрдЧреНрд░рд╣ рд▓реЗрдВ:
 {_id: 0, username: "user0", age: 20} {_id: 1, username: "user1", age: 42} {_id: 2, username: "user2", age: 28} 

рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛рдУрдВ рдХреЗ рдпрд╛рджреГрдЪреНрдЫрд┐рдХ рдирдореВрдиреЗ рдХреЗ рдорд╛рдирдХ рд╡рд┐рдЪрд▓рди рдХреА рдЧрдгрдирд╛ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП, рд╣рдо рдкрд╣рд▓реЗ 100 рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛рдУрдВ рдХреЗ рдирдореВрдиреЗ рдХреЗ рд▓рд┐рдП $sample рдСрдкрд░реЗрдЯрд░ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рддреЗ рд╣реИрдВ, рдФрд░ рдлрд┐рд░ рдЧрдгрдирд╛ рдХреЗ рд▓рд┐рдП $stdDevSamp рдЙрдкрдпреЛрдЧ рдХрд░рддреЗ рд╣реИрдВред
 db.users.aggregate( [ { $sample: { size: 100 } }, { $group: { _id: null, ageStdDev: { $stdDevSamp: "$age" } } } ] ) 

рдкрд░рд┐рдгрд╛рдо:
 { "_id" : null, "ageStdDev" : 7.811258386185771 } 


рдирдП рдПрдХрддреНрд░реАрдХрд░рдг рдЕрдВрдХрдЧрдгрд┐рддреАрдп рдСрдкрд░реЗрдЯрд░


$sqrt - рд╡рд░реНрдЧрдореВрд▓ рдХреА рдЧрдгрдирд╛ рдХрд░рддрд╛ рд╣реИред
рд╡рд╛рдХреНрдп-рд╡рд┐рдиреНрдпрд╛рд╕
 { $sqrt: <number> } 

рд╕рд░рд▓ рдЙрджрд╛рд╣рд░рдг:
 { $sqrt: 25 } 5 { $sqrt: 30 } 5.477225575051661 { $sqrt: null } null 

рджрд╕реНрддрд╛рд╡реЗрдЬреЛрдВ рдХреЗ рд╕рд╛рде points рдХрд╛ рдПрдХ рд╕рдВрдЧреНрд░рд╣ рд╣реИ:
 { _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} } 

рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдЙрджрд╛рд╣рд░рдг p1 рдФрд░ p2 рдмреАрдЪ рдХреА рджреВрд░реА рдХреА рдЧрдгрдирд╛ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП $sqrt рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рддрд╛ рд╣реИ:
 db.points.aggregate([ { $project: { distance: { $sqrt: { $add: [ { $pow: [ { $subtract: [ "$p2.y", "$p1.y" ] }, 2 ] }, { $pow: [ { $subtract: [ "$p2.x", "$p1.x" ] }, 2 ] } ] } } } } ]) 

рдЖрдЙрдЯрдкреБрдЯ рдирд┐рдореНрди рдкрд░рд┐рдгрд╛рдо рд╣реИ:
 { "_id" : 1, "distance" : 5.830951894845301 } { "_id" : 2, "distance" : 5 } { "_id" : 3, "distance" : 4 } 

$abs - рдХрд┐рд╕реА рд╕рдВрдЦреНрдпрд╛ рдХрд╛ рдирд┐рд░рдкреЗрдХреНрд╖ рдорд╛рди рд▓реМрдЯрд╛рддрд╛ рд╣реИ
рд╡рд╛рдХреНрдп-рд╡рд┐рдиреНрдпрд╛рд╕
 { $abs: <number> } 

рдПрдХ рд╕рд░рд▓ рдЙрджрд╛рд╣рд░рдг:
 { $abs: -1 } 1 { $abs: 1 } 1 { $abs: null } null 


ratings рд╕рдВрдЧреНрд░рд╣ рдореЗрдВ рджрд╕реНрддрд╛рд╡реЗрдЬ рд╣реИрдВ:
 { _id: 1, start: 5, end: 8 } { _id: 2, start: 4, end: 4 } { _id: 3, start: 9, end: 7 } { _id: 4, start: 6, end: 7 } 

рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдЙрджрд╛рд╣рд░рдг рдкреНрд░рд╛рд░рдВрднрд┐рдХ рдФрд░ рдЕрдВрддрд┐рдо рд░реЗрдЯрд┐рдВрдЧ рдХреЗ рдмреАрдЪ рдЕрдВрддрд░ рдХреА рдЧрдгрдирд╛ рдХрд░рддрд╛ рд╣реИ:
 db.ratings.aggregate([ { $project: { delta: { $abs: { $subtract: [ "$start", "$end" ] } } } } ]) 

рдЖрдЙрдЯрдкреБрдЯ:
 { "_id" : 1, "delta" : 3 } { "_id" : 2, "delta" : 0 } { "_id" : 3, "delta" : 2 } { "_id" : 4, "delta" : 1 } 


$log - рд▓реЙрдЧрд░рд┐рджрдо рдХреА рдЧрдгрдирд╛ рдХрд░рддрд╛ рд╣реИред
рд╡рд╛рдХреНрдп-рд╡рд┐рдиреНрдпрд╛рд╕
 { $log: [ <number>, <base> ] } 

рдХрд┐рд╕реА рд╕рдВрдЦреНрдпрд╛ рдХреЗ рд▓рдШреБрдЧрдгрдХ рдХреА рдЧрдгрдирд╛ рдХрд░рддрд╛ рд╣реИ, рдФрд░ рдкрд░рд┐рдгрд╛рдо рдХреЛ рдПрдХ рдЕрдВрд╢ рдХреЗ рд░реВрдк рдореЗрдВ рд▓реМрдЯрд╛рддрд╛ рд╣реИред
examples рдХрд╛ рдПрдХ рд╕рдВрдЧреНрд░рд╣ рд╣реИ:
 { _id: 1, positiveInt: 5 } { _id: 2, positiveInt: 2 } { _id: 3, positiveInt: 23 } { _id: 4, positiveInt: 10 } 

рд╕рдХрд╛рд░рд╛рддреНрдордХ рдЙрджрд╛рд╣рд░рдг рдХрд╛ рдкреНрд░рддрд┐рдирд┐рдзрд┐рддреНрд╡ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдЖрд╡рд╢реНрдпрдХ рдмрд┐рдЯреНрд╕ рдХреА рд╕рдВрдЦреНрдпрд╛ рдирд┐рд░реНрдзрд╛рд░рд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдирд┐рдореНрди рдЙрджрд╛рд╣рд░рдг log2 рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рддрд╛ рд╣реИ
 . 
:
{ "_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/In192870/


All Articles