设为首页 加入收藏

TOP

《转》CentOS7安装MongoDB3.0服务器(3.0的优势)(二)
2015-11-21 01:53:17 来源: 作者: 【 】 浏览:6
Tags:《转》 CentOS7 安装 MongoDB3.0 服务器 3.0 优势
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-
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇复盘eygle在甲骨文大会上演讲中的.. 下一篇WAS_集群部署应用遭遇ADMA0085E和..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: