mongdb使用指南2

mongdb使用指南2
#查看当前库存在哪些表
> show tables
col
system.indexes

#查看集合中字段值符合某个类型的记录
> db.typeTable.find().pretty();
{ "_id" : ObjectId("583f8b06fc157fa1aa75af5e"), "content" : "123456" }
{ "_id" : ObjectId("583f8b0cfc157fa1aa75af5f"), "content" : 123456 }
> db.typeTable.find({title:{ $type:2}});
> db.typeTable.find({content:{ $type:2}});
{ "_id" : ObjectId("583f8b06fc157fa1aa75af5e"), "content" : "123456" }

#类型表
类型    数字    备注
Double    1    
String    2    
Object    3    
Array    4    
Binary data    5    
Undefined    6    已废弃。
Object id    7    
Boolean    8    
Date    9    
Null    10    
Regular Expression    11    
JavaScript    13    
Symbol    14    
JavaScript (with scope)    15    
32-bit integer    16    
Timestamp    17    
64-bit integer    18    
Min key    255    Query with -1.
Max key    127    

#获取集合中前n条数据
> db.typeTable.find();
{ "_id" : ObjectId("583f8b06fc157fa1aa75af5e"), "content" : "123456" }
{ "_id" : ObjectId("583f8b0cfc157fa1aa75af5f"), "content" : 123456 }
{ "_id" : ObjectId("583f8bc4fc157fa1aa75af60"), "name" : "wgd" }
{ "_id" : ObjectId("583f8bccfc157fa1aa75af61"), "age" : "18" }
> db.typeTable.find().limit(2);
{ "_id" : ObjectId("583f8b06fc157fa1aa75af5e"), "content" : "123456" }
{ "_id" : ObjectId("583f8b0cfc157fa1aa75af5f"), "content" : 123456 }

#从某一条开始,获取n条数据( skip(n):跳过n条记录 )
> db.typeTable.find().limit(2).skip(2);
{ "_id" : ObjectId("583f8bc4fc157fa1aa75af60"), "name" : "wgd" }
{ "_id" : ObjectId("583f8bccfc157fa1aa75af61"), "age" : "18" }

#对集合中的记录进行排序(默认是1,至上而下,从小到大,-1是倒序)
> db.typeTable.find();
{ "_id" : ObjectId("583f8b06fc157fa1aa75af5e"), "content" : "123456" }
{ "_id" : ObjectId("583f8b0cfc157fa1aa75af5f"), "content" : 123456 }
{ "_id" : ObjectId("583f8bc4fc157fa1aa75af60"), "name" : "wgd" }
{ "_id" : ObjectId("583f8bccfc157fa1aa75af61"), "age" : "18" }
> db.typeTable.find().sort({'_id':1});
{ "_id" : ObjectId("583f8b06fc157fa1aa75af5e"), "content" : "123456" }
{ "_id" : ObjectId("583f8b0cfc157fa1aa75af5f"), "content" : 123456 }
{ "_id" : ObjectId("583f8bc4fc157fa1aa75af60"), "name" : "wgd" }
{ "_id" : ObjectId("583f8bccfc157fa1aa75af61"), "age" : "18" }
> db.typeTable.find().sort({'_id':-1});
{ "_id" : ObjectId("583f8bccfc157fa1aa75af61"), "age" : "18" }
{ "_id" : ObjectId("583f8bc4fc157fa1aa75af60"), "name" : "wgd" }
{ "_id" : ObjectId("583f8b0cfc157fa1aa75af5f"), "content" : 123456 }
{ "_id" : ObjectId("583f8b06fc157fa1aa75af5e"), "content" : "123456" }

#使用聚合函数
    #创建一张表

> db.typeTable.find();
{ "_id" : ObjectId("583f8f3094e243957b2b2d8c"), "title" : "MongoDB Overview", "description" : "MongoDB is no sql database", "by_user" : "w3cschool.cc", "url" : "http://www.w3cschool.cc", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("583f8f4c94e243957b2b2d8d"), "title" : "NoSQL Overview", "description" : "No sql database is very fast", "by_user" : "w3cschool.cc", "url" : "http://www.w3cschool.cc", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 10 }
{ "_id" : ObjectId("583f8f6694e243957b2b2d8e"), "title" : "Neo4j Overview", "description" : "Neo4j is no sql database", "by_user" : "Neo4j", "url" : "http://www.neo4j.com", "tags" : [ "neo4j", "database", "NoSQL" ], "likes" : 750 }
    #计算每个作者所写的文章数
    #语法 表.aggregate([{ $group: { _id:"$被分组的字段名",num_tutorial:{$sum:是否使用该聚合函数(0:不使用,1:使用) } } }]);
> db.typeTable.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
{ "_id" : "w3cschool.cc", "num_tutorial" : 2 }
{ "_id" : "Neo4j", "num_tutorial" : 1 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值