Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- EMBEDDING AND REFERENCING EXAMPLES:
- situation: we have a collection of stores. Those stores have some items in offer. We also have sales in those stores (persons buying).
- store {
- name: "...",
- address: "...",
- items:[item_id*]
- }
- items {
- id: ...,
- name: ...,
- price: ...
- }
- sales {
- name: ...,
- person_id: ...,
- items:[{item_id:"...", name:..., price:...},...]
- store:"store_name"
- }
- // cmd
- ./mongod
- mkdir
- ./mongod -dbpath dbdata
- //new cmd
- .\mongo
- > db.getCollectionNames()
- > db.createCollection("AAA")
- { "ok" : 1 }
- > db.test.insert({tekstowy:"document"})
- WriteResult({ "nInserted" : 1 })
- > db.getCollectionNames()
- [ "AAA", "test" ]
- > db.AAA.drop()
- true
- > show dbs
- admin 0.000GB
- config 0.000GB
- local 0.000GB
- test 0.000GB
- > use local
- switched to db local
- > db.getCollectionNames()
- [ "startup_log" ]
- > use test
- switched to db test
- //2 te same
- > db.test.insert({testowy:"dokument"})
- WriteResult({ "nInserted" : 1 })
- > db.test.insert({testowy:"dokument"})
- WriteResult({ "nInserted" : 1 })
- > db.test.find()
- { "_id" : ObjectId("5dcd5f188a30ed9789e14cec"), "tekstowy" : "document" }
- { "_id" : ObjectId("5dcd60558a30ed9789e14ced"), "testowy" : "dokument" }
- { "_id" : ObjectId("5dcd60568a30ed9789e14cee"), "testowy" : "dokument" }
- > db.test.insert({"_id":1, test:2})
- WriteResult({ "nInserted" : 1 })
- > db.test.find()
- { "_id" : ObjectId("5dcd5f188a30ed9789e14cec"), "tekstowy" : "document" }
- { "_id" : ObjectId("5dcd60558a30ed9789e14ced"), "testowy" : "dokument" }
- { "_id" : ObjectId("5dcd60568a30ed9789e14cee"), "testowy" : "dokument" }
- { "_id" : 1, "test" : 2 }
- //duplicate key
- > db.test.insert({"_id":1, test:3})
- WriteResult({
- "nInserted" : 0,
- "writeError" : {
- "code" : 11000,
- "errmsg" : "E11000 duplicate key error collection: test.test index: _id_ dup key: { _id: 1.0 }"
- }
- })
- > db.test.insert({
- ... name:"Anthony",
- ... surname:"Smith",
- ... age: 13,
- ... address: {
- ... street: "Queen's road",
- ... number: 15,
- ... "zip-code": "45-236"
- ... },
- ... phones: [ "423-453-4534", "222-222-2222" ],
- ... asspcoates: [{name: "Bart", surname: "Smith"}, {name: "Joan", surname: "Bean"}]
- ... })
- WriteResult({ "nInserted" : 1 })
- > db.test.deleteMany({})
- { "acknowledged" : true, "deletedCount" : 5 }
- > db.test.find().pretty()
- {
- "_id" : ObjectId("5dcd63178a30ed9789e14cf0"),
- "name" : "Anthony",
- "surname" : "Smith",
- "age" : 13,
- "address" : {
- "street" : "Queen's road",
- "number" : 15,
- "zip-code" : "45-236"
- },
- "phones" : [
- "423-453-4534",
- "222-222-2222"
- ],
- "asspcoates" : [
- {
- "name" : "Bart",
- "surname" : "Smith"
- },
- {
- "name" : "Joan",
- "surname" : "Bean"
- }
- ]
- }
- > db.test.insertMany([{}.{}.{}])
- > db.test.find().pretty()
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf5"), "name" : "Abe", "surname" : "Kendricks" }
- > db.test.find({age:47}, {name:1, surname:1})
- > db.test.find({age:47}, {"_id":0, name:1, surname:1})
- { "name" : "Abe", "surname" : "Kendricks" }
- > db.test.find({age: {$gte:40}}, {"_id":0, name:1, surname:1, age:1})
- { "name" : "Bart", "surname" : "Smith", "age" : 52 }
- { "name" : "Joan", "surname" : "Bean", "age" : 43 }
- { "name" : "Abe", "surname" : "Kendricks", "age" : 47 }
- > db.test.find({age: {$gte:40}, surname:"Smith"}, {"_id":0, name:1, surname:1, age:1})
- { "name" : "Bart", "surname" : "Smith", "age" : 52 }
- > db.test.find({age: {$gte:40, $lte:50}}, {"_id":0, name:1, surname:1, age:1})
- { "name" : "Joan", "surname" : "Bean", "age" : 43 }
- { "name" : "Abe", "surname" : "Kendricks", "age" : 47 }
- > db.test.find({address:{street:"Queen's road", number:15, "zip-code":"45-236"}})
- { "_id" : ObjectId("5dcd63178a30ed9789e14cf0"), "name" : "Anthony", "surname" : "Smith", "age" : 13, "address" : { "street" : "Queen's road", "number" : 15, "zip-code" : "45-236" }, "phones" : [ "423-453-4534", "222-222-2222" ], "asspcoates" : [ { "name" : "Bart", "surname" : "Smith" }, { "name" : "Joan", "surname" : "Bean" } ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf2"), "name" : "Joan", "surname" : "Bean", "age" : 43, "address" : { "street" : "Queen's road", "number" : 15, "zip-code" : "45-236" }, "phones" : [ "123-456-7890", "222-222-2222", "423-453-4534" ], "associates" : [ { "surname" : "Smith", "name" : "Anthony" }, { "name" : "Jonny", "surname" : "Bean" } ], "marks" : null }
- //nie dziala
- > db.test.find({address:{street:"Queen's road", number:15, "zip-code":"45-236"}})
- > db.test.find({address:{"zip-code": "35-236", street:"Queen's road", number:15}}).pretty()
- //dziala
- > db.test.find({"address.zip-code":"45-236"})
- > db.test.find({"address.street":"Queen's road"})
- // znajdz dokumenty, ktore maja dokladnie te numery w tej kolejnosci
- > db.test.find({phones:["423-453-4534", "222-222-2222"]})
- { "_id" : ObjectId("5dcd63178a30ed9789e14cf0"), "name" : "Anthony", "surname" : "Smith", "age" : 13, "address" : { "street" : "Queen's road", "number" : 15, "zip-code" : "45-236" }, "phones" : [ "423-453-4534", "222-222-2222" ], "asspcoates" : [ { "name" : "Bart", "surname" : "Smith" }, { "name" : "Joan", "surname" : "Bean" } ] }
- // znajdz dokumenty w ktorych sa te 2 numery w ogole
- > db.test.find({phones:{$all:["423-453-4534", "222-222-2222"]}})
- { "_id" : ObjectId("5dcd63178a30ed9789e14cf0"), "name" : "Anthony", "surname" : "Smith", "age" : 13, "address" : { "street" : "Queen's road", "number" : 15, "zip-code" : "45-236" }, "phones" : [ "423-453-4534", "222-222-2222" ], "asspcoates" : [ { "name" : "Bart", "surname" : "Smith" }, { "name" : "Joan", "surname" : "Bean" } ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf2"), "name" : "Joan", "surname" : "Bean", "age" : 43, "address" : { "street" : "Queen's road", "number" : 15, "zip-code" : "45-236" }, "phones" : [ "123-456-7890", "222-222-2222", "423-453-4534" ], "associates" : [ { "surname" : "Smith", "name" : "Anthony" }, { "name" : "Jonny", "surname" : "Bean" } ], "marks" : null }
- > db.test.find({phones:"222-222-2222"})
- { "_id" : ObjectId("5dcd63178a30ed9789e14cf0"), "name" : "Anthony", "surname" : "Smith", "age" : 13, "address" : { "street" : "Queen's road", "number" : 15, "zip-code" : "45-236" }, "phones" : [ "423-453-4534", "222-222-2222" ], "asspcoates" : [ { "name" : "Bart", "surname" : "Smith" }, { "name" : "Joan", "surname" : "Bean" } ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf2"), "name" : "Joan", "surname" : "Bean", "age" : 43, "address" : { "street" : "Queen's road", "number" : 15, "zip-code" : "45-236" }, "phones" : [ "123-456-7890", "222-222-2222", "423-453-4534" ], "associates" : [ { "surname" : "Smith", "name" : "Anthony" }, { "name" : "Jonny", "surname" : "Bean" } ], "marks" : null }
- > db.test.find({phones:["222-222-2222"]})
- >
- > db.test.find({marks: {$gt:4}})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf3"), "name" : "Jonny", "surname" : "Bean", "age" : 23, "address" : { "street" : "Mulholand drive", "number" : 5, "zip-code" : "45-236" }, "phones" : [ "423-453-4534", "999-342-2344" ], "associates" : [ { "name" : "All", "surname" : "o'Brian" }, { "name" : "Joan", "surname" : "Bean" } ], "marks" : [ 1, 3, 7, 8 ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf4"), "name" : "Burt", "surname" : "O'Donnel", "age" : 39, "address" : { "street" : "Dunno ally", "number" : 13, "zip-code" : "11-435" }, "phones" : [ "543-546-5355", "111-111-1111" ], "associates" : [ { "name" : "Kathy", "surname" : "Jenkins" } ], "marks" : [ 4, 5 ] }
- //zle:
- > db.test.find({marks: {$gte:4, $lte:6}})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf3"), "name" : "Jonny", "surname" : "Bean", "age" : 23, "address" : { "street" : "Mulholand drive", "number" : 5, "zip-code" : "45-236" }, "phones" : [ "423-453-4534", "999-342-2344" ], "associates" : [ { "name" : "All", "surname" : "o'Brian" }, { "name" : "Joan", "surname" : "Bean" } ], "marks" : [ 1, 3, 7, 8 ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf4"), "name" : "Burt", "surname" : "O'Donnel", "age" : 39, "address" : { "street" : "Dunno ally", "number" : 13, "zip-code" : "11-435" }, "phones" : [ "543-546-5355", "111-111-1111" ], "associates" : [ { "name" : "Kathy", "surname" : "Jenkins" } ], "marks" : [ 4, 5 ] }
- //lepiej:
- > db.test.find({marks: {$gte:4, $lte:6}},{"marks.$":1})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf3"), "marks" : [ 7 ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf4"), "marks" : [ 4 ] }
- //dobrze
- > db.test.find({marks: {$elemMatch: {$gte:4,$lte:6}}} )
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf4"), "name" : "Burt", "surname" : "O'Donnel", "age" : 39, "address" : { "street" : "Dunno ally", "number" : 13, "zip-code" : "11-435" }, "phones" : [ "543-546-5355", "111-111-1111" ], "associates" : [ { "name" : "Kathy", "surname" : "Jenkins" } ], "marks" : [ 4, 5 ] }
- //nie ma ocen
- > db.test.find({$or: [{marks: {$size:0}}]})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf5"), "name" : "Abe", "surname" : "Kendricks", "age" : 47, "address" : { "street" : "King's highway", "number" : 1, "zip-code" : "98-435" }, "phones" : [ "774-425-4324", "554-542-8997" ], "associates" : [ { "name" : "Joan", "surname" : "Black" }, { "name" : "Patty", "surname" : "Fuller" } ], "marks" : [ ] }
- > db.test.find({$or: [{marks: {$size:0}}, {marks:null}]}, {marks:1})
- { "_id" : ObjectId("5dcd63178a30ed9789e14cf0") }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf1") }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf2"), "marks" : null }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf5"), "marks" : [ ] }
- // maja oceny
- > db.test.find({$nor: [{marks: {$size:0}}, {marks:null}]}, {marks:1})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf3"), "marks" : [ 1, 3, 7, 8 ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf4"), "marks" : [ 4, 5 ] }
- //oceny nie sa nullami
- > db.test.find({marks: {$not:{$type:10}}}, {marks:1})
- { "_id" : ObjectId("5dcd63178a30ed9789e14cf0") }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf1") }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf3"), "marks" : [ 1, 3, 7, 8 ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf4"), "marks" : [ 4, 5 ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf5"), "marks" : [ ] }
- // nie ma ocen
- > db.test.find({marks: {$exists: false}}, {marks:1})
- { "_id" : ObjectId("5dcd63178a30ed9789e14cf0") }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf1") }
- > db.test.find({associates:{name:"Anthony", surname:"Smith"}},{associates:1})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf1"), "associates" : [ { "name" : "Anthony", "surname" : "Smith" }, { "name" : "Joan", "surname" : "Bean" } ] }
- > db.test.find({"associates.name":"Anthony", "associates.surname":"Smith"},{associates:1})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf1"), "associates" : [ { "name" : "Anthony", "surname" : "Smith" }, { "name" : "Joan", "surname" : "Bean" } ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf2"), "associates" : [ { "surname" : "Smith", "name" : "Anthony" }, { "name" : "Jonny", "surname" : "Bean" } ] }
- > db.test.find({associates:{$elemMatch:{name:"Anthony", surname: "Smith"}}},{associates:1})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf1"), "associates" : [ { "name" : "Anthony", "surname" : "Smith" }, { "name" : "Joan", "surname" : "Bean" } ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf2"), "associates" : [ { "surname" : "Smith", "name" : "Anthony" }, { "name" : "Jonny", "surname" : "Bean" } ] }
- //replace
- > db.test.replaceOne({name:"Anthony"}, {name:"AntonyNowy"})
- > db.test.deleteOne({name:"AntonyNowy"})
- { "acknowledged" : true, "deletedCount" : 1 }
- //update
- > db.test.updateOne({name: "Anthony"}, {$set: {name:"Antony"}})
- //poprawa associatesów
- //nie dziala
- > db.test.updateOne({"associates.name":"Anthony"}, {$set: {"associates.name":"Antony"}})
- //dziala z jednym
- > db.test.updateOne({"associates.name":"Anthony"}, {$set: {"associates.$.name":"Antony"}})
- //dziala z wieloma
- > db.test.updateOne({"associates.name":"Anthony"}, {$set: {"associates.$[].name":"Antony"}})
- //zwiekszenie wieku wszystich o 1
- > db.test.updateMany({},{$inc:{age:1}})
- { "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
- //wszystkie <30 będą równe 30
- > db.test.updateMany({},{$max:{age:30}})
- { "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 1 }
- > db.test.find({},{age:1})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf1"), "age" : 53 }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf2"), "age" : 44 }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf3"), "age" : 30 }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf4"), "age" : 40 }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf5"), "age" : 48 }
- //dodaj oceny 5 i 7
- > db.test.updateMany({marks:{$exists:true, $type:4}},{$push:{marks:{$each:[5,7]}}})
- { "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
- > db.test.find({},{marks:1})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf1") }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf2"), "marks" : null }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf3"), "marks" : [ 1, 3, 7, 8, 5, 7 ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf4"), "marks" : [ 4, 5, 5, 7 ] }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf5"), "marks" : [ 5, 7 ] }
- //usun oceny
- > db.test.updateMany({},{$unset:{marks:null}})
- { "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 4 }
- > db.test.find({},{marks:1})
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf1") }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf2") }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf3") }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf4") }
- { "_id" : ObjectId("5dcd63b08a30ed9789e14cf5") }
- //usuniecie wszystkiego
- > db.test.deleteMany({})
- { "acknowledged" : true, "deletedCount" : 5 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement