mongodb insert / save (commit?) / update / remove / find (=select) / drop data (= document = row)


C:\Users\Gin>cd C:\Program Files\MongoDB\Server\3.2\bin

C:\Program Files\MongoDB\Server\3.2\bin>mongo
2016-08-30T14:00:06.711+0900 I CONTROL  [main] Hotfix KB2731284 or later update
is not installed, will zero-out data files
MongoDB shell version: 3.2.9
connecting to: test
> use test
switched to db test
> m= {ename:"smith"}
{ "ename" : "smith" }
> n={empno:1101}
{ "empno" : 1101 }
> db.things.save(m)
WriteResult({ "nInserted" : 1 })
> db.things.save(n)
WriteResult({ "nInserted" : 1 })
> db.things.find()
{ "_id" : ObjectId("57c51315f360f38d060f44c4"), "ename" : "smith" }
{ "_id" : ObjectId("57c51321f360f38d060f44c5"), "empno" : 1101 }
> db.things.insert({empno:1102, ename:"king"})
WriteResult({ "nInserted" : 1 })
> db.things.find()
{ "_id" : ObjectId("57c51315f360f38d060f44c4"), "ename" : "smith" }
{ "_id" : ObjectId("57c51321f360f38d060f44c5"), "empno" : 1101 }
{ "_id" : ObjectId("57c5134bf360f38d060f44c6"), "empno" : 1102, "ename" : "king"
 }
> for (var p=1103;p<=1110;p++) db.things.save({n:p, m:"test"})<<(save: modify certain DOCUMENT)
WriteResult({ "nInserted" : 1 })
> db.things.find()
{ "_id" : ObjectId("57c51315f360f38d060f44c4"), "ename" : "smith" }
{ "_id" : ObjectId("57c51321f360f38d060f44c5"), "empno" : 1101 }
{ "_id" : ObjectId("57c5134bf360f38d060f44c6"), "empno" : 1102, "ename" : "king"
 }
{ "_id" : ObjectId("57c513a1f360f38d060f44c7"), "n" : 1103, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44c8"), "n" : 1104, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44c9"), "n" : 1105, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44ca"), "n" : 1106, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44cb"), "n" : 1107, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44cc"), "n" : 1108, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44cd"), "n" : 1109, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44ce"), "n" : 1110, "m" : "test" }
> db.things.update({n:1103}, {$set: {ename:"standford", dept:"research"}})   <<(update: modify certain FIELD)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.things.update({n:1104}, {$set:{ename:"john", dept:"inventory"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.things.find()
{ "_id" : ObjectId("57c51315f360f38d060f44c4"), "ename" : "smith" }
{ "_id" : ObjectId("57c51321f360f38d060f44c5"), "empno" : 1101 }
{ "_id" : ObjectId("57c5134bf360f38d060f44c6"), "empno" : 1102, "ename" : "king"
 }
{ "_id" : ObjectId("57c513a1f360f38d060f44c7"), "n" : 1103, "m" : "test", "ename
" : "standford", "dept" : "research" }
{ "_id" : ObjectId("57c513a1f360f38d060f44c8"), "n" : 1104, "m" : "test", "ename
" : "john", "dept" : "inventory" }
{ "_id" : ObjectId("57c513a1f360f38d060f44c9"), "n" : 1105, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44ca"), "n" : 1106, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44cb"), "n" : 1107, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44cc"), "n" : 1108, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44cd"), "n" : 1109, "m" : "test" }
{ "_id" : ObjectId("57c513a1f360f38d060f44ce"), "n" : 1110, "m" : "test" }
> it
no cursor
> db.things.remove({m:"test"})
WriteResult({ "nRemoved" : 8 })
> db.things.find();
{ "_id" : ObjectId("57c51315f360f38d060f44c4"), "ename" : "smith" }
{ "_id" : ObjectId("57c51321f360f38d060f44c5"), "empno" : 1101 }
{ "_id" : ObjectId("57c5134bf360f38d060f44c6"), "empno" : 1102, "ename" : "king"
 }
> db.things.remove({})
WriteResult({ "nRemoved" : 3 })
> db.things.find()
> db.things.drop()
true
>
Previous
Next Post »