扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
NOSQL的数据存储模型
键值模型:(key-avalue存储)
优点:查找速度快
缺点:数据无结构,通常只被当作字符串或二进制数据
应用场景:内容缓存
实例:Redis,Dynamo
列式模型
数据模型:数据按列存储,将同一列数据存在一起。
优点:查找迅速,可扩展性强,易于实现分布式
缺点:功能相对SQL很有限
应用场景:分布式文件系统或分布式存储
实例:Bigtable(google),Cassandra(facebook),HBase(hadoop),Hypertable
文档模型
数据模型:与键值模型类似,value指向结构化数据
优点:数据格式要求不严格,无需事先定义结构
缺点:查询性能不高,缺乏统一查询语法
应用场景:web应用
实例:MongoDB,CouchDB
图式模型
数据模型:图结构模型
优点:利用图结构相关算法提高性能,并满足特珠场景应用需求
缺点:难以实现分布式,功能有定向性
应用场景:社交网络,推荐系统,关系图谱
实例:Neo4j
ww.nosql.database.org
mongodb
C/S
mongod
mongo-->mongod
mongo>use testdb(不需要分号;结尾)
mongo>db.mycollection.insert()
面向collection的数据库
数据库:但数据库无须创建
表:行<-->集合:文档
集合无须事先定义;
安装与配置
1、 创建 /etc/yum.repos.d/mongodb-org-3.0.repo文件
[root@hadoop2 ~]# vim /etc/yum.repos.d/mongodb-org-3.0.repo
[mongodb-org-3.0]
name=MongoDB Repostitory
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/
gpgcheck=0
enabled=1
#[mongodb-org-2.6]
#name=MongoDB 2.6 Repository
#baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
#gpgcheck=0
#enabled=1
2、配置
[root@hadoop2 ~]# yum install -y mongodb-org
[root@hadoop2 ~]# mkdir -p /mongod/data
[root@hadoop2 ~]# chown -R mongod.mongod /mongo
[root@hadoop2 ~]# vim /etc/mongod.conf
# mongod.conf
#where to log
logpath=/var/log/mongodb/mongod.log
logappend=true
# fork and run in background
fork=true
#port=27017
dbpath=/mongo/data/ ##改一下数据存放路径
# location of pidfile
pidfilepath=/var/run/mongodb/mongod.pid
[root@hadoop2 mongo]# service mongod start
Starting mongod: [确定]
3、基础操作
[root@hadoop2 mongo]# mongo
MongoDB shell version: 3.0.4
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2015-07-21T14:58:55.518+0800 I CONTROL [initandlisten]
2015-07-21T14:58:55.518+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-07-21T14:58:55.518+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-07-21T14:58:55.518+0800 I CONTROL [initandlisten]
2015-07-21T14:58:55.519+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-07-21T14:58:55.519+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-07-21T14:58:55.519+0800 I CONTROL [initandlisten]
2015-07-21T14:58:55.519+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
2015-07-21T14:58:55.519+0800 I CONTROL [initandlisten]
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
> show co
CountDownLatch connect( copyDbpath(
compare( connectionURLTheSame( copyFile(
compareOn( constructor
> show dbs
local 0.078GB
> for(i=1;i<=100;i++) db.testcoll.insert({Name: "User"+i,Age: i,Gender: "M",PreferBook: ["first book","Second book"]})
WriteResult({ "nInserted" : 1 })
> db.testcoll.find()
{ "_id" : ObjectId("55adefd39883f07d0866c3b4"), "Name" : "User1", "Age" : 1, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b5"), "Name" : "User2", "Age" : 2, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b6"), "Name" : "User3", "Age" : 3, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b7"), "Name" : "User4", "Age" : 4, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b8"), "Name" : "User5", "Age" : 5, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b9"), "Name" : "User6", "Age" : 6, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ba"), "Name" : "User7", "Age" : 7, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bb"), "Name" : "User8", "Age" : 8, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bc"), "Name" : "User9", "Age" : 9, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bd"), "Name" : "User10", "Age" : 10, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3be"), "Name" : "User11", "Age" : 11, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bf"), "Name" : "User12", "Age" : 12, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c0"), "Name" : "User13", "Age" : 13, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c1"), "Name" : "User14", "Age" : 14, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c2"), "Name" : "User15", "Age" : 15, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c3"), "Name" : "User16", "Age" : 16, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c4"), "Name" : "User17", "Age" : 17, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c5"), "Name" : "User18", "Age" : 18, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c6"), "Name" : "User19", "Age" : 19, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c7"), "Name" : "User20", "Age" : 20, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
Type "it" for more
> it
{ "_id" : ObjectId("55adefd49883f07d0866c3c8"), "Name" : "User21", "Age" : 21, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c9"), "Name" : "User22", "Age" : 22, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ca"), "Name" : "User23", "Age" : 23, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3cb"), "Name" : "User24", "Age" : 24, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3cc"), "Name" : "User25", "Age" : 25, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3cd"), "Name" : "User26", "Age" : 26, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ce"), "Name" : "User27", "Age" : 27, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3cf"), "Name" : "User28", "Age" : 28, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3d0"), "Name" : "User29", "Age" : 29, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3d1"), "Name" : "User30", "Age" : 30, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3d2"), "Name" : "User31", "Age" : 31, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3d3"), "Name" : "User32", "Age" : 32, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3d4"), "Name" : "User33", "Age" : 33, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3d5"), "Name" : "User34", "Age" : 34, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3d6"), "Name" : "User35", "Age" : 35, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3d7"), "Name" : "User36", "Age" : 36, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3d8"), "Name" : "User37", "Age" : 37, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3d9"), "Name" : "User38", "Age" : 38, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3da"), "Name" : "User39", "Age" : 39, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3db"), "Name" : "User40", "Age" : 40, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
Type "it" for more
> it
{ "_id" : ObjectId("55adefd49883f07d0866c3dc"), "Name" : "User41", "Age" : 41, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3dd"), "Name" : "User42", "Age" : 42, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3de"), "Name" : "User43", "Age" : 43, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3df"), "Name" : "User44", "Age" : 44, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3e0"), "Name" : "User45", "Age" : 45, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3e1"), "Name" : "User46", "Age" : 46, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3e2"), "Name" : "User47", "Age" : 47, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3e3"), "Name" : "User48", "Age" : 48, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3e4"), "Name" : "User49", "Age" : 49, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3e5"), "Name" : "User50", "Age" : 50, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3e6"), "Name" : "User51", "Age" : 51, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3e7"), "Name" : "User52", "Age" : 52, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3e8"), "Name" : "User53", "Age" : 53, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3e9"), "Name" : "User54", "Age" : 54, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ea"), "Name" : "User55", "Age" : 55, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3eb"), "Name" : "User56", "Age" : 56, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ec"), "Name" : "User57", "Age" : 57, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ed"), "Name" : "User58", "Age" : 58, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ee"), "Name" : "User59", "Age" : 59, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ef"), "Name" : "User60", "Age" : 60, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
Type "it" for more
> db.testcoll.find().limit(2)
{ "_id" : ObjectId("55adefd39883f07d0866c3b4"), "Name" : "User1", "Age" : 1, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b5"), "Name" : "User2", "Age" : 2, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
> db.testcoll.remove({Age:10})
WriteResult({ "nRemoved" : 1 })
> db.testcoll.find().limit(15)
{ "_id" : ObjectId("55adefd39883f07d0866c3b4"), "Name" : "User1", "Age" : 1, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b5"), "Name" : "User2", "Age" : 2, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b6"), "Name" : "User3", "Age" : 3, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b7"), "Name" : "User4", "Age" : 4, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b8"), "Name" : "User5", "Age" : 5, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b9"), "Name" : "User6", "Age" : 6, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ba"), "Name" : "User7", "Age" : 7, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bb"), "Name" : "User8", "Age" : 8, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bc"), "Name" : "User9", "Age" : 9, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3be"), "Name" : "User11", "Age" : 11, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bf"), "Name" : "User12", "Age" : 12, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c0"), "Name" : "User13", "Age" : 13, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c1"), "Name" : "User14", "Age" : 14, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c2"), "Name" : "User15", "Age" : 15, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c3"), "Name" : "User16", "Age" : 16, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
> db.testcoll.re
db.testcoll.reIndex( db.testcoll.renameCollection(
db.testcoll.remove(
> db.testcoll.remove({Name:"User14"})
WriteResult({ "nRemoved" : 1 })
> db.testcoll.find().limit(15)
{ "_id" : ObjectId("55adefd39883f07d0866c3b4"), "Name" : "User1", "Age" : 1, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b5"), "Name" : "User2", "Age" : 2, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b6"), "Name" : "User3", "Age" : 3, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b7"), "Name" : "User4", "Age" : 4, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b8"), "Name" : "User5", "Age" : 5, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b9"), "Name" : "User6", "Age" : 6, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ba"), "Name" : "User7", "Age" : 7, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bb"), "Name" : "User8", "Age" : 8, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bc"), "Name" : "User9", "Age" : 9, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3be"), "Name" : "User11", "Age" : 11, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bf"), "Name" : "User12", "Age" : 12, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c0"), "Name" : "User13", "Age" : 13, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c2"), "Name" : "User15", "Age" : 15, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c3"), "Name" : "User16", "Age" : 16, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c4"), "Name" : "User17", "Age" : 17, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
> db.testcoll.update({Name:"User12"},{$set:{Age: 32}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.testcoll.find().limit(15)
{ "_id" : ObjectId("55adefd39883f07d0866c3b4"), "Name" : "User1", "Age" : 1, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b5"), "Name" : "User2", "Age" : 2, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b6"), "Name" : "User3", "Age" : 3, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b7"), "Name" : "User4", "Age" : 4, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b8"), "Name" : "User5", "Age" : 5, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b9"), "Name" : "User6", "Age" : 6, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ba"), "Name" : "User7", "Age" : 7, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bb"), "Name" : "User8", "Age" : 8, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bc"), "Name" : "User9", "Age" : 9, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3be"), "Name" : "User11", "Age" : 11, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bf"), "Name" : "User12", "Age" : 32, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c0"), "Name" : "User13", "Age" : 13, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c2"), "Name" : "User15", "Age" : 15, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c3"), "Name" : "User16", "Age" : 16, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c4"), "Name" : "User17", "Age" : 17, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
> db.testcoll.co
db.testcoll.constructor db.testcoll.copyTo(
db.testcoll.convertToCapped( db.testcoll.count(
db.testcoll.convertToSingleObject(
> db.testcoll.count()
98
> db.testcoll.find({Age: {$gte:93}})
{ "_id" : ObjectId("55adefd49883f07d0866c410"), "Name" : "User93", "Age" : 93, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c411"), "Name" : "User94", "Age" : 94, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c412"), "Name" : "User95", "Age" : 95, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c413"), "Name" : "User96", "Age" : 96, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c414"), "Name" : "User97", "Age" : 97, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c415"), "Name" : "User98", "Age" : 98, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c416"), "Name" : "User99", "Age" : 99, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c417"), "Name" : "User100", "Age" : 100, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
>
> db.testcoll.find({Age: {$gte:93}},{Name:1,Age:1})
{ "_id" : ObjectId("55adefd49883f07d0866c410"), "Name" : "User93", "Age" : 93 }
{ "_id" : ObjectId("55adefd49883f07d0866c411"), "Name" : "User94", "Age" : 94 }
{ "_id" : ObjectId("55adefd49883f07d0866c412"), "Name" : "User95", "Age" : 95 }
{ "_id" : ObjectId("55adefd49883f07d0866c413"), "Name" : "User96", "Age" : 96 }
{ "_id" : ObjectId("55adefd49883f07d0866c414"), "Name" : "User97", "Age" : 97 }
{ "_id" : ObjectId("55adefd49883f07d0866c415"), "Name" : "User98", "Age" : 98 }
{ "_id" : ObjectId("55adefd49883f07d0866c416"), "Name" : "User99", "Age" : 99 }
{ "_id" : ObjectId("55adefd49883f07d0866c417"), "Name" : "User100", "Age" : 100 }
> db.testcoll.find({$and: [{Age:{$gt: 61}},{Age: {$lt:80}}]})
{ "_id" : ObjectId("55adefd49883f07d0866c3f1"), "Name" : "User62", "Age" : 62, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3f2"), "Name" : "User63", "Age" : 63, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3f3"), "Name" : "User64", "Age" : 64, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3f4"), "Name" : "User65", "Age" : 65, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3f5"), "Name" : "User66", "Age" : 66, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3f6"), "Name" : "User67", "Age" : 67, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3f7"), "Name" : "User68", "Age" : 68, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3f8"), "Name" : "User69", "Age" : 69, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3f9"), "Name" : "User70", "Age" : 70, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3fa"), "Name" : "User71", "Age" : 71, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3fb"), "Name" : "User72", "Age" : 72, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3fc"), "Name" : "User73", "Age" : 73, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3fd"), "Name" : "User74", "Age" : 74, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3fe"), "Name" : "User75", "Age" : 75, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ff"), "Name" : "User76", "Age" : 76, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c400"), "Name" : "User77", "Age" : 77, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c401"), "Name" : "User78", "Age" : 78, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c402"), "Name" : "User79", "Age" : 79, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
> db.testcoll.insert({Name: "User101",Age: 101,Gender: "F",Address: "Beijing,Chian"})
WriteResult({ "nInserted" : 1 })
> db.testcoll.find({Address: {$exists: true}})
{ "_id" : ObjectId("55adf56044061abfb9ba93f2"), "Name" : "User101", "Age" : 101, "Gender" : "F", "Address" : "Beijing,Chian" }
> db.testcoll.find({Address: {$exists: false}})
{ "_id" : ObjectId("55adefd39883f07d0866c3b4"), "Name" : "User1", "Age" : 1, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b5"), "Name" : "User2", "Age" : 2, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b6"), "Name" : "User3", "Age" : 3, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b7"), "Name" : "User4", "Age" : 4, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b8"), "Name" : "User5", "Age" : 5, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3b9"), "Name" : "User6", "Age" : 6, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3ba"), "Name" : "User7", "Age" : 7, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bb"), "Name" : "User8", "Age" : 8, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bc"), "Name" : "User9", "Age" : 9, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3be"), "Name" : "User11", "Age" : 11, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3bf"), "Name" : "User12", "Age" : 32, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c0"), "Name" : "User13", "Age" : 13, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c2"), "Name" : "User15", "Age" : 15, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c3"), "Name" : "User16", "Age" : 16, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c4"), "Name" : "User17", "Age" : 17, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c5"), "Name" : "User18", "Age" : 18, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c6"), "Name" : "User19", "Age" : 19, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c7"), "Name" : "User20", "Age" : 20, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c8"), "Name" : "User21", "Age" : 21, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c3c9"), "Name" : "User22", "Age" : 22, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
Type "it" for more
> db.testcoll.update({Age: {$gt:80}},{$set: {Gender: "F"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.testcoll.find({Age: {$gte: 80}})
{ "_id" : ObjectId("55adf56044061abfb9ba93f2"), "Name" : "User101", "Age" : 101, "Gender" : "F", "Address" : "Beijing,Chian" }
{ "_id" : ObjectId("55adefd49883f07d0866c403"), "Name" : "User80", "Age" : 80, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c404"), "Name" : "User81", "Age" : 81, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c405"), "Name" : "User82", "Age" : 82, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c406"), "Name" : "User83", "Age" : 83, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c407"), "Name" : "User84", "Age" : 84, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c408"), "Name" : "User85", "Age" : 85, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c409"), "Name" : "User86", "Age" : 86, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40a"), "Name" : "User87", "Age" : 87, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40b"), "Name" : "User88", "Age" : 88, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40c"), "Name" : "User89", "Age" : 89, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40d"), "Name" : "User90", "Age" : 90, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40e"), "Name" : "User91", "Age" : 91, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40f"), "Name" : "User92", "Age" : 92, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c410"), "Name" : "User93", "Age" : 93, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c411"), "Name" : "User94", "Age" : 94, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c412"), "Name" : "User95", "Age" : 95, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c413"), "Name" : "User96", "Age" : 96, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c414"), "Name" : "User97", "Age" : 97, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c415"), "Name" : "User98", "Age" : 98, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
Type "it" for more
> db.testcoll.update({Age: {$gt:85}},{$set: {Gender: "F"}},{multi:true})
WriteResult({ "nMatched" : 16, "nUpserted" : 0, "nModified" : 15 })
> db.testcoll.find({Age: {$gte: 80}})
{ "_id" : ObjectId("55adf56044061abfb9ba93f2"), "Name" : "User101", "Age" : 101, "Gender" : "F", "Address" : "Beijing,Chian" }
{ "_id" : ObjectId("55adefd49883f07d0866c403"), "Name" : "User80", "Age" : 80, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c404"), "Name" : "User81", "Age" : 81, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c405"), "Name" : "User82", "Age" : 82, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c406"), "Name" : "User83", "Age" : 83, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c407"), "Name" : "User84", "Age" : 84, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c408"), "Name" : "User85", "Age" : 85, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c409"), "Name" : "User86", "Age" : 86, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40a"), "Name" : "User87", "Age" : 87, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40b"), "Name" : "User88", "Age" : 88, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40c"), "Name" : "User89", "Age" : 89, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40d"), "Name" : "User90", "Age" : 90, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40e"), "Name" : "User91", "Age" : 91, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40f"), "Name" : "User92", "Age" : 92, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c410"), "Name" : "User93", "Age" : 93, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c411"), "Name" : "User94", "Age" : 94, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c412"), "Name" : "User95", "Age" : 95, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c413"), "Name" : "User96", "Age" : 96, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c414"), "Name" : "User97", "Age" : 97, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c415"), "Name" : "User98", "Age" : 98, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
Type "it" for more
> db.testcoll.update({Name: "User92"},{$unset:{PreferBooks: ""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.testcoll.find({Age: {$gte: 80}})
{ "_id" : ObjectId("55adf56044061abfb9ba93f2"), "Name" : "User101", "Age" : 101, "Gender" : "F", "Address" : "Beijing,Chian" }
{ "_id" : ObjectId("55adefd49883f07d0866c403"), "Name" : "User80", "Age" : 80, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c404"), "Name" : "User81", "Age" : 81, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c405"), "Name" : "User82", "Age" : 82, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c406"), "Name" : "User83", "Age" : 83, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c407"), "Name" : "User84", "Age" : 84, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c408"), "Name" : "User85", "Age" : 85, "Gender" : "M", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c409"), "Name" : "User86", "Age" : 86, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40a"), "Name" : "User87", "Age" : 87, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40b"), "Name" : "User88", "Age" : 88, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40c"), "Name" : "User89", "Age" : 89, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40d"), "Name" : "User90", "Age" : 90, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40e"), "Name" : "User91", "Age" : 91, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c40f"), "Name" : "User92", "Age" : 92, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c410"), "Name" : "User93", "Age" : 93, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c411"), "Name" : "User94", "Age" : 94, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c412"), "Name" : "User95", "Age" : 95, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c413"), "Name" : "User96", "Age" : 96, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c414"), "Name" : "User97", "Age" : 97, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
{ "_id" : ObjectId("55adefd49883f07d0866c415"), "Name" : "User98", "Age" : 98, "Gender" : "F", "PreferBook" : [ "first book", "Second book" ] }
Type "it" for more
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流