mongodb数据创建索引_MongoDB数据创建方法及查询例子

时间:2020-10-08  来源:MongoDB  阅读:

1.连接数据库

[root@localhost ~]# /Apps/mongo/bin/mongo
MongoDB shell version: 1.8.1
connecting to: test
>

2.插入记录

>j = { name : "mongo" };
{"name" : "mongo"}
> t = { x : 3 };
{ "x" : 3    }
> db.things.save(j);
> db.things.save(t);
> db.things.find();
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
>
 
> for( var i = 1; i < 10; i++ ) db.things.save( { x:4, j:i } ); > db.things.find();
{"name" : "mongo" , "_id" : ObjectId("497cf60751712cf7758fbdbb")}
{"x" : 3 , "_id" : ObjectId("497cf61651712cf7758fbdbc")}
{"x" : 4 , "j" : 1 , "_id" : ObjectId("497cf87151712cf7758fbdbd")}
{"x" : 4 , "j" : 2 , "_id" : ObjectId("497cf87151712cf7758fbdbe")}
{"x" : 4 , "j" : 3 , "_id" : ObjectId("497cf87151712cf7758fbdbf")}
{"x" : 4 , "j" : 4 , "_id" : ObjectId("497cf87151712cf7758fbdc0")}
{"x" : 4 , "j" : 5 , "_id" : ObjectId("497cf87151712cf7758fbdc1")}
{"x" : 4 , "j" : 6 , "_id" : ObjectId("497cf87151712cf7758fbdc2")}
{"x" : 4 , "j" : 7 , "_id" : ObjectId("497cf87151712cf7758fbdc3")}
{"x" : 4 , "j" : 8 , "_id" : ObjectId("497cf87151712cf7758fbdc4")}
 

3.查询记录

3.1普通查询

>var cursor = db.things.find();
> while (cursor.hasNext()) printjson(cursor.next());
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 }

上面的例子显示了游标风格的迭代输出.  hasNext()  函数告诉我们是否还有数据,  如果有则
可以调用  next()  函数.

当我们使用的是  JavaScript shell,  可以用到JS的特性, forEach  就可以输出游标了.  下面的例
子就是使用  forEach()  来循环输出:    forEach() 必须定义一个函数供每个游标元素调用.

> db.things.find().forEach(printjson);
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 } ...
 

在  MongoDB shell  里,  我们也可以把游标当作数组来用:

> var cursor = db.things.find();
> printjson(cursor[4]);
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
 

使用游标时候请注意占用内存的问题,  特别是很大的游标对象,  有可能会内存溢出.  所以应
该用迭代的方式来输出.  下面的示例则是把游标转换成真实的数组类型:

> var arr = db.things.find().toArray();
>arr[5];
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
 

3.2条件查询

下面的示例就是说明如何执行一个类似SQL的查询,  并演示了怎么在  MongoDB  里实现.  这
是在MongoDB shell里查询,  当然你也可以用其他的应用程序驱动或者语言来实现:

  SELECT * FROM things WHERE name="mongo"
> db.things.find({name:"mongo"}).forEach(printjson);
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
 
SELECT * FROM things WHERE x=4
>db.things.find({x:4}).forEach(printjson);
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 }

查询条件是  { a:A, b:B,  …  }  类似  “where a==A and b==B and …”.上面显示的是所有的元素,  当然我们也可以返回特定的元素,  类似于返回表里某字段的值,
只需要在  find({x:4})  里指定元素的名字

SELECT j FROM things WHERE x=4
> db.things.find({x:4}, {j:true}).forEach(printjson);
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "j" : 1 }
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "j" : 2 }
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "j" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "j" : 4 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "j" : 5 }
 

查询表中存在某字段的项:

db.things.find({},{"***":true}) //***处为查找的字段
 

3.3findOne语法

为了方便考虑,  MongoDB  shell 避免游标可能带来的开销,  提供一个findOne() 函数.  这个函
数和  find()  函数一样,  不过它返回的是游标里第一条数据,  或者返回null,即空数据.

作为一个例子,  name=”mongo”  可以用很多方法来实现,  可以用  next()  来循环游标或者当
做数组返回第一个元素.

但是用  findOne()  方法则更简单和高效:

> printjson(db.things.findOne({name:"mongo"}));
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
 

3.4通过limit限制结果集数量

如果需要限制结果集的长度,  那么可以调用  limit  方法.
这是强烈推荐解决性能问题的方法,  就是通过限制条数来减少网络传输,  例如:

> db.things.find().limit(3);
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
 

4.修改记录

将name是mongo的记录的name修改为mongo_new

> db.things.update({name:"mongo"},{$set:{name:"mongo_new"}});

我们来查询一下是否改过来了

> db.things.find();                                                                                   
{ "_id" : ObjectId("4faa9e7dedd27e6d86d86371"), "x" : 3 }
{ "_id" : ObjectId("4faa9e7bedd27e6d86d86370"), "name" : "mongo_new" }
 
5.删除记录

将用户name是mongo_new的记录从集合things中删除

>db.things.remove({name:"mongo_new"});
> db.things.find();                                        
{ "_id" : ObjectId("4faa9e7dedd27e6d86d86371"), "x" : 3 }

经验证,该记录确实被删除了
 

6、内存释放

>use admin
 
>db.runCommand({closeAllDatabases:1})

mongodb数据创建索引_MongoDB数据创建方法及查询例子

http://m.bbyears.com/shujuku/103400.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新