码迷,mamicode.com
首页 > 数据库 > 详细

mongoDB之find()操作

时间:2015-03-10 22:51:47      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

一 创建数据库

 执行操作进入mongodb   使用参数模式   也可以使用 db.auth()来验证。

root@timeless-HP-Pavilion-g4-Notebook-PC:/usr/local/apache243/htdocs# /usr/local/mongodb/mongo  -uroot -pxxxxx localhost:27017/admin

 下面是第一次创建新的数据库

> use DEMO
switched to db DEMO
> db.user.insert({name:"zhaoxingzhuang",age:18,sex:"man",location:{province:"shandong",city:"jinan",road:"shandaroad-47"}})
WriteResult({ "nInserted" : 1 })
> db.user.find()  //全部查询
{ "_id" : ObjectId("54feea0a26be41dca9db0d23"), "name" : "zhaoxingzhuang", "age" : 18, "sex" : "man", "location" : { "province" : "shandong", "city" : "jinan", "road" : "shandaroad-47" } }
> 

 

 继续执行插入操作 

1、简单查询  find({field:"value"},{field:0或者1}) ,前边表示查询的条件  后边表示  查询的字段  0表示 不显示该字段  1表示只显示这个字段

> db.user.insert({name:"wangping",age:26,sex:"woman",location:{city:"beijing",road:"zhongguonongyedaxue"}})
WriteResult({ "nInserted" : 1 })
> db.user.find()
{ "_id" : ObjectId("54feea0a26be41dca9db0d23"), "name" : "zhaoxingzhuang", "age" : 18, "sex" : "man", "location" : { "province" : "shandong", "city" : "jinan", "road" : "shandaroad-47" } }
{ "_id" : ObjectId("54feead226be41dca9db0d24"), "name" : "wangping", "age" : 26, "sex" : "woman", "location" : { "city" : "beijing", "road" : "zhongguonongyedaxue" } }
> db.user.find({name:"wangping"})// 查询name="wangping"
{ "_id" : ObjectId("54feead226be41dca9db0d24"), "name" : "wangping", "age" : 26, "sex" : "woman", "location" : { "city" : "beijing", "road" : "zhongguonongyedaxue" } }

//字段选取    类似 sql 语句中的 select age from XXX;
> db.user.find({name:"wangping"},{age:0})     //表示不显示 age字段
{ "_id" : ObjectId("54feead226be41dca9db0d24"), "name" : "wangping", "sex" : "woman", "location" : { "city" : "beijing", "road" : "zhongguonongyedaxue" } }

> db.user.find({name:"wangping"},{age:1})    // 表示只取  age字段
{ "_id" : ObjectId("54feead226be41dca9db0d24"), "age" : 26 }
>

 2、条件查询 > $gt , < $lt ,<=   $lte ,>=  $gte。

> db.user.find({age:{$gt:20}})   //查询年龄大于20岁的
{ "_id" : ObjectId("54feead226be41dca9db0d24"), "name" : "wangping", "age" : 26, "sex" : "woman", "location" : { "city" : "beijing", "road" : "zhongguonongyedaxue" } }
> 

 3、$all操作只对数组有效(类似于 $in),    表示要查询的 $all:[1,2,5,6]    1256  必须全部在要查询的数组中 , 而$in 表示只要有一个就符合查询条件。

> db.user.insert({name:"gaofei",age:24,grade:[1,2,5,6],location:{province:"shandong",city:"jiyang",road:"zuozhuangzhen"}})
WriteResult({ "nInserted" : 1 })
> db.user.find(grade:{$all:[1,2,5,6]})
2015-03-10T21:19:20.164+0800 SyntaxError: Unexpected token :
> db.user.find({grade:{$all:[1,2,5,6]}})    //$all  查询条件
{ "_id" : ObjectId("54feef3626be41dca9db0d25"), "name" : "gaofei", "age" : 24, "grade" : [ 1, 2, 5, 6 ], "location" : { "province" : "shandong", "city" : "jiyang", "road" : "zuozhuangzhen" } }
> 

 $all   $in  只是用在数组上

> db.user.find({name:{$all:[1,2,5,6]}})   //字段不是 数组  的情况
> db.user.find({name:{$in:[1]}})
> db.user.find({grade:{$in:[1]}})       //$in 使用查询
{ "_id" : ObjectId("54feef3626be41dca9db0d25"), "name" : "gaofei", "age" : 24, "grade" : [ 1, 2, 5, 6 ], "location" : { "province" : "shandong", "city" : "jiyang", "road" : "zuozhuangzhen" } }
> 

 

4、skip()   limit()  count()  sort()  使用

 其中  skip  跟 limit  使用 类似于   sql语句中的  select * from user limit 0,10;

> db.user.find()    //按照添加顺序列出来
{ "_id" : ObjectId("54feea0a26be41dca9db0d23"), "name" : "zhaoxingzhuang", "age" : 18, "sex" : "man", "location" : { "province" : "shandong", "city" : "jinan", "road" : "shandaroad-47" } }
{ "_id" : ObjectId("54feead226be41dca9db0d24"), "name" : "wangping", "age" : 26, "sex" : "woman", "location" : { "city" : "beijing", "road" : "zhongguonongyedaxue" } }
{ "_id" : ObjectId("54feef3626be41dca9db0d25"), "name" : "gaofei", "age" : 24, "grade" : [ 1, 2, 5, 6 ], "location" : { "province" : "shandong", "city" : "jiyang", "road" : "zuozhuangzhen" } }
//limit   从零开始 查询两条 
> db.user.find().limit(2)
{ "_id" : ObjectId("54feea0a26be41dca9db0d23"), "name" : "zhaoxingzhuang", "age" : 18, "sex" : "man", "location" : { "province" : "shandong", "city" : "jinan", "road" : "shandaroad-47" } }
{ "_id" : ObjectId("54feead226be41dca9db0d24"), "name" : "wangping", "age" : 26, "sex" : "woman", "location" : { "city" : "beijing", "road" : "zhongguonongyedaxue" } }
> db.user.skip(1).limit()
2015-03-10T21:26:17.430+0800 TypeError: Property ‘skip‘ of object DEMO.user is not a function
> db.user.find().skip(1).limit()  //从第一条开始
{ "_id" : ObjectId("54feead226be41dca9db0d24"), "name" : "wangping", "age" : 26, "sex" : "woman", "location" : { "city" : "beijing", "road" : "zhongguonongyedaxue" } }
{ "_id" : ObjectId("54feef3626be41dca9db0d25"), "name" : "gaofei", "age" : 24, "grade" : [ 1, 2, 5, 6 ], "location" : { "province" : "shandong", "city" : "jiyang", "road" : "zuozhuangzhen" } }
> db.user.count()   //统计数据总量
3
> db.user.find().sort(age:1)   // 即得要把排序的内容放在 {} 中
2015-03-10T21:27:49.863+0800 SyntaxError: Unexpected token :
> db.user.find().sort({age:1})
{ "_id" : ObjectId("54feea0a26be41dca9db0d23"), "name" : "zhaoxingzhuang", "age" : 18, "sex" : "man", "location" : { "province" : "shandong", "city" : "jinan", "road" : "shandaroad-47" } }
{ "_id" : ObjectId("54feef3626be41dca9db0d25"), "name" : "gaofei", "age" : 24, "grade" : [ 1, 2, 5, 6 ], "location" : { "province" : "shandong", "city" : "jiyang", "road" : "zuozhuangzhen" } }
{ "_id" : ObjectId("54feead226be41dca9db0d24"), "name" : "wangping", "age" : 26, "sex" : "woman", "location" : { "city" : "beijing", "road" : "zhongguonongyedaxue" } }
> 

 

mongoDB之find()操作

标签:

原文地址:http://www.cnblogs.com/timelesszhuang/p/4328478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!