l/tutorial/insert-documents/ http://docs.mongodb.org/manual/reference/method/db.collection.insert/
> db.users.insert(
... {
... name:"zhang san",
... age:26,
... city:"bei jing"
... }
... )
WriteResult({ "nInserted" : 1 })
> db.users.insert(
... {
... _id:1,
... name:"zhang san",
... age:26,
... city:"bei jing"
... }
... )
WriteResult({ "nInserted" : 1 })
> db.users.insert(
... {
... _id:1,
... name:"zhang san",
... age:26,
... city:"bei jing"
... }
... )
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error index: test.users.$_id_ dup key: { : 1.0 }"
}
})
> db.users.insert(
... {
... _id:2,
... name:"li si",
... age:28,
... city:"shang hai"
... }
... )
WriteResult({ "nInserted" : 1 })
数据可以没有主键_id,如果没有,会自动生成一个。如果设置了_id主键,就必须不重复。 否则报主键冲突:“E11000 duplicate key error index: test.users.$_id_ dup key: { : 1.0 }”
2.2,更新数据:
http://docs.mongodb.org/manual/tutorial/modify-documents/
> db.users.update(
... {_id:2},
... {
... $set: {
... city:"guang zhou"
... }
... }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.update(
... {_id:3},
... {
... $set: {
... city:"si chuan"
... }
... },
... { upsert: true }
... )
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 3 })
更新使用update,如果增加{ upsert: true },则表示没有查询到数据直接插入。
2.3,删除:
http://docs.mongodb.org/manual/tutorial/remove-documents/
> db.users.remove({_id:3})
WriteResult({ "nRemoved" : 1 })
> db.users.remove({_id:4})
WriteResult({ "nRemoved" : 0 })
查询到数据才进行删除,并且返回删除数量。
2.4,查询:
http://docs.mongodb.org/manual/tutorial/query-documents/
> db.users.find({age:{ $gt: 26}})
{ "_id" : 2, "name" : "li si", "age" : 28, "city" : "guang zhou" }
> db.users.find({age:{ $gt: 25}})
{ "_id" : ObjectId("5540adf29b0f52a6786de216"), "name" : "zhang san", "age" : 26, "city" : "bei jing" }
{ "_id" : 1, "name" : "zhang san", "age" : 26, "city" : "bei jing" }
{ "_id" : 2, "name" : "li si", "age" : 28, "city" : "guang zhou" }
#查询全部数据
> db.users.find()
{ "_id" : ObjectId("5540adf29b0f52a6786de216"), "name" : "zhang san", "age" : 26, "city" : "bei jing" }
{ "_id" : 1, "name" : "zhang san", "age" : 26, "city" : "bei jing" }
{ "_id" : 2, "name" : "li si", "age" : 28, "city" : "guang zhou" }
2.5,更多方法
db.collection.aggregate() db.collection.count() db.collection.copyTo() db.collection.createIndex() db.collection.getIndexStats() db.collection.indexStats() db.collection.dataSize() db.collection.distinct() db.collection.drop() db.collection.dropIndex() db.collection.dropIndexes() db.collection.ensureIndex() db.collection.explain() db.collection.find() db.collection.findAndModify() db.collection.findOne() db.collection.getIndexes() db.collection.getShardDistribution() db.collection.getShardVersion() db.collection.group() db.collection.insert() db.collection.isCapped() db.collection.mapReduce() db.collection.reIndex() db.collection.remove() db.collection.renameCollection() db.collection.save() db.collection.stats() db.collection.storageSize() db.collection.totalSize() db.collection.totalIndexSize() db.collection.update() db.collection.validate()
3,MongoDB可视化工具
http://www.robomongo.org/ 
使用可视化工具,方便使用MongoDB管理。 首先要修改下端口和ip vi /etc/mongod.conf
delphi has-