第二章:MongoDB简单的增删改查

来源:互联网 发布:淘宝模特拍照24姿势 编辑:程序博客网 时间:2024/04/30 04:47


一: Insert操作

①  单条插入


var single={
"name":"tom",
"password":"123456",
"age":24,
"address":{"province":"hubei","city":"shiyan"},
"loves":["eat","play","drink"]
}
var single2={
"name":"tom2",
"password":"123456",
"age":20,
"address":{"province":"shenzhen","city":"dong     guan"},
"loves":["eat","play","drink"]
}
var single3={
"name":"tom3",
"password":"123456",
"age":20,
"address":{"province":"hunan","city":"changsha"},
"loves":["eat","play","drink"]
}

var single4={
"name":"tom4",
"password":"123456",
"age":20,
"address":{"province":"guangdong","city":"shiyan"},
"loves":["eat","play","drink"]
}
var single5={
"name":"tom",
"password":"123456",
"age":44,
"address":{"province":"hubei","city":"shiyan"},
"loves":["eat","play","drink"]
}

> single.name='tom'  修改name为tom
tom
> single.age=22  修改年龄为22

db.user.insert(single)
WriteResult({ "nInserted" : 1 })

db.user.find()
{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }


二:Find操作

       日常开发中,查询用的比较多。

      ①: >, >=, <, <=, !=, =。

      ②:And,OR,In,NotIn

这些操作在mongodb里面都封装好了,下面就一一介绍:

  <1>"$gt", "$gte", "$lt", "$lte", "$ne", "没有特殊关键字",这些跟上面是一一对应的,举几个例子。

/ * find age>22 */ 
db.user.find({"age":{$gt:22}})  


{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }

/ * find age<22 */ 
db.user.find({"age":{$lt:22}})   
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }

/ * find age!=22 */ 
db.user.find({"age":{$ne:22}})  

{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }


/ * find age==22 */ 
db.user.find({"age":20})  

{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "
eat", "play", "drink" ] }


<2> "无关键字“, "$or", "$in","$nin" 同样我也是举几个例子

/* find name='tom' && province='hubei'*/  查询名字为tom 并且 province=湖北的记录

db.user.find({"name":"tom2","address.province":"hubei"})

{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }

/* find province='hubei' || province='hunan'*/  查询 province='hubei' 或者 province='hunan' 的记录

db.user.find({$or:[{"address.province":"hubei"},{"address.province":"hunan"}]})

{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }

/* find province in  ["hubei","hunan"]*/ 

db.user.find({"address.province":{$in:["hubei","hunan"]}})

{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473382f2122f816c6dec18"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }

/* find province  not in  ["hubei","hunan"]*/ 

db.user.find({"address.province":{$nin:["hubei","hunan"]}})

{ "_id" : ObjectId("5547369cf2122f816c6dec19"), "name" : "tom3", "password" : "123456", "age" : 20, "address" : { "province" : "guangdong", "city" : "shiyan" }, "loves" :[ "eat", "play", "drink" ] }

<3> 在mongodb中还有一个特殊的匹配,那就是“正则表达式”,功能很强大。

/* find name startwith 't' and  endwith 'm' */ 

db.user.find({"name":/^t/,"name":/m$/})

{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }


<4>$where 处理很复杂。

/* find name='tom2' */
 
db.user.find({"name":"tom2"})

{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473382f2122f816c6dec18"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }


db.user.find({$where:function(){return this.name=='tom2'}})

{ "_id" : ObjectId("5547302ddd647b87c8344727"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473380f2122f816c6dec17"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55473382f2122f816c6dec18"), "name" : "tom2", "password" : "123456", "age" : 20, "address" : { "province" : "hunan", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }

三:Update操作

    <1> 整体更新

 /* update tom age=33 */

 >  var tage=db.user.findOne({"name":'tom'})
 >  tage.age=33
 >  db.user.update({"name":"tom"},tage)

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find()
{ "_id" : ObjectId("55472bc5dd647b87c8344725"), "name" : "tom", "password" : "123456", "age" : 33, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }
{ "_id" : ObjectId("55472fa6dd647b87c8344726"), "name" : "tom", "password" : "123456", "age" : 36, "address" : { "province" : "hubei", "city" : "shiyan" }, "loves" : [ "eat", "play", "drink" ] }


  <2>上传大对象文件
 mongofiles put testfile  

 D:\Program Files\mongodb\Server\3.1\bin>mongofiles put c:/quxian.sql   将c盘里面的quanxian.sql 这个文件上传到数据库

 2015-05-05T15:25:39.442+0800    connected to: localhost


> show collections
fs.chunks    --fs.chunks  

fs.files         --fs.files 
persion
system.indexes
user
users
>

>db.fs.files.find()   --查看files的详细信息
{ "_id" : ObjectId("554870730c94ed15dc000001"), "chunkSize" : 261120, "uploadDate" : ISODate("2015-05-05T07:25:39.995Z"), "length" : 223846, "md5" :
0757d86c8677", "filename" : "c:/quxian.sql" }

> db.fs.chunks.find()   --查看文件的内容

 
 
 
 
0 0
原创粉丝点击