mongodb练习(及排序投影)

练习一:

 1.> use my_test;

switched to db my_test >

show dbs

admin 0.000GB config 0.000GB local 0.000GB test 0.000GB > > > >

2.>db.users.insert({ name:"sunxingzhe" })

WriteResult({ "nInserted" : 1 })

 

 

 

3. > db.users.find(); { "_id" : ObjectId("656835fc288e8614266b7fd4"), "name" : "sunxingzhe" } { "_id" : ObjectId("65683693288e8614266b7fd5"), "name" : "sunxingzhe" } > db.users.find({}); { "_id" : ObjectId("656835fc288e8614266b7fd4"), "name" : "sunxingzhe" } { "_id" : ObjectId("65683693288e8614266b7fd5"), "name" : "sunxingzhe" }

 

 

4.再插入一个人员:

> db.users.insert({ name:"tangsheng" })
WriteResult({ "nInserted" : 1 })
5.查看文档
> db.users.find()
{ "_id" : ObjectId("656835fc288e8614266b7fd4"), "name" : "sunxingzhe" }
{ "_id" : ObjectId("65683693288e8614266b7fd5"), "name" : "sunxingzhe" }
{ "_id" : ObjectId("65683f71288e8614266b7fd6"), "name" : "zhubajie" }
{ "_id" : ObjectId("656841548d73ce8b5b591119"), "name" : "tangsheng" }
6.统计文档数量:
> db.users.find().count()
4
> db.users.find({name:"sunxingzhe"}).count()
2
7.查询文档中名称为“sunxingzhe”的文档:
> db.users.find({name:"sunxingzhe"})
{ "_id" : ObjectId("656835fc288e8614266b7fd4"), "name" : "sunxingzhe" }
{ "_id" : ObjectId("65683693288e8614266b7fd5"), "name" : "sunxingzhe" }
>
8.更新数据:添加sunxingzhe地址address为“花果山”
db.users.update({name:"sunxingzhe"},{$set:{address:"huaguoshan"}})##########更新一个数据
db.users.updateMany({name:"sunxingzhe"},{$set:{address:"huaguoshan"}})###########更新多条数据

 9用shaheshang替换“tangsheng“

 db.users.replaceOne({name:"tangsheng"},{name:"shaheshang"})
 

 10.删除sunwukong的地址属性:address

##### db.users.update({name:"sunxingzhe"},{$unset:{address:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({name:"sunwukong"})
> db.users.find({name:"sunxingzhe"})
{ "_id" : ObjectId("656835fc288e8614266b7fd4"), "name" : "sunxingzhe" }
{ "_id" : ObjectId("65683693288e8614266b7fd5"), "name" : "sunxingzhe", "address" : "huaguoshan" }
##删除多个数据db.users.updateMany({name:"sunxingzhe"},{$unset:{address:1}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 1 }
 

 

11.添加属性hobby:       cities:["beijing","shanghai","shenzhen"]

                                      movies:["sanguo","hero"]

 

 db.users.update(
{name:"sunxingzhe"} ,
{$set:
    { hobby: {    cities:["beijing","shanghai","shenzhen"],
           movies:["sanguo","hero"]}
}
}

 )

#####更新所有数据:db.users.updateMany( {name:"sunxingzhe"} , {$set:     { hobby: {    cities:["beijing","shanghai","shenzhen"],            movies:["sanguo","hero"]} } }  )
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 1 }
> db.users.find()
{ "_id" : ObjectId("656835fc288e8614266b7fd4"), "name" : "sunxingzhe", "hobby" : { "cities" : [ "beijing", "shanghai", "shenzhen" ], "movies" : [ "sanguo", "hero" ] } }
{ "_id" : ObjectId("65683693288e8614266b7fd5"), "name" : "sunxingzhe", "hobby" : { "cities" : [ "beijing", "shanghai", "shenzhen" ], "movies" : [ "sanguo", "hero" ] } }
{ "_id" : ObjectId("65683f71288e8614266b7fd6"), "name" : "zhubajie" }
{ "_id" : ObjectId("656841548d73ce8b5b591119"), "name" : "shaheshang" }
{ "_id" : ObjectId("656939502440990653977f87") }
>
12.给tangsheng加一个爱好:hobby: {movies:["xiyouji","honglou"]}
db.users.update(
{name:"tangsheng"} ,
{$set:
    { hobby: {   movies:["honglou","xiyouji"] }
}
}

 )

 13.查询喜欢hero的文档:

 

 14.向tangsheng中添加一个新电影Interstellar

 

> db.users.update({name:"tangsheng"},{$push:{"hobby.movies":["honglou"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

 

 

> db.users.update({name:"tangsheng"},{$addToSet:{"hobby.movies":'Interstellar'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({'name':'tangsheng'}) { "_id" : ObjectId("65694d6f12a41d2d93144215"), "name" : "tangsheng", "hobby" : { "movies" : [ [ "honglou" ], "Interstellar" ] } } > db.users.update({name:"tangsheng"},{$addToSet:{"hobby.movies":'Interstellar'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.users.find({'name':'tangsheng'}) { "_id" : ObjectId("65694d6f12a41d2d93144215"), "name" : "tangsheng", "hobby" : { "movies" : [ [ "honglou" ], "Interstellar" ] } } > db.users.update({name:"tangsheng"},{$addToSet:{"hobby.movies":'Interstellar'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.users.find({'name':'tangsheng'}) { "_id" : ObjectId("65694d6f12a41d2d93144215"), "name" : "tangsheng", "hobby" : { "movies" : [ [ "honglou" ], "Interstellar" ] } } > db.users.update({name:"tangsheng"},{$push:{"hobby.movies":'xiyouji'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find({'name':'tangsheng'}) { "_id" : ObjectId("65694d6f12a41d2d93144215"), "name" : "tangsheng", "hobby" : { "movies" : [ [ "honglou" ], "Interstellar", "xiyouji" ] } } >

15.删除喜欢北京的用户:

db.users.remove({"hobby.cities":"beijing"});

WriteResult({ "nRemoved" : 2 })

 

 16.删除user集合:

db.users.remove({})

db.users.drop()

 17.向numbers中插入20000条数据

 for (var i=1;i<= 2000;i++){

db.number.insert({num:i})

}
20000条语句执行以下:

第一种方式,时间长一点:

#####for (var i=1;i<= 20000;i++){  db.number.insert({num:i})  }

 第二种方式:时间较短一些:
var a = [];

for (var i=1;i<= 20000;i++) {

a.push({num:i});

}

db.number.insert(a)

 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

18.查询数字为500的数据:

db.number.find({num:500})

 19.查询大于5000的数据:

db.number.find({num:{$gt:5000}}).count()  大于5000有15000条数据

db.number.find({num:{$eq:5000}}).count()   等于5000的数据有一个

 20小于30的数据

> db.number.find({num:{$lt:30}});
{ "_id" : ObjectId("656992be9b4547ef74cfeb97"), "num" : 1 }
{ "_id" : ObjectId("656992be9b4547ef74cfeb98"), "num" : 2 }
{ "_id" : ObjectId("656992be9b4547ef74cfeb99"), "num" : 3 }
{ "_id" : ObjectId("656992be9b4547ef74cfeb9a"), "num" : 4 }
{ "_id" : ObjectId("656992be9b4547ef74cfeb9b"), "num" : 5 }
{ "_id" : ObjectId("656992be9b4547ef74cfeb9c"), "num" : 6 }
{ "_id" : ObjectId("656992be9b4547ef74cfeb9d"), "num" : 7 }
{ "_id" : ObjectId("656992be9b4547ef74cfeb9e"), "num" : 8 }
{ "_id" : ObjectId("656992be9b4547ef74cfeb9f"), "num" : 9 }
{ "_id" : ObjectId("656992be9b4547ef74cfeba0"), "num" : 10 }
{ "_id" : ObjectId("656992be9b4547ef74cfeba1"), "num" : 11 }
{ "_id" : ObjectId("656992be9b4547ef74cfeba2"), "num" : 12 }
{ "_id" : ObjectId("656992be9b4547ef74cfeba3"), "num" : 13 }
{ "_id" : ObjectId("656992be9b4547ef74cfeba4"), "num" : 14 }
{ "_id" : ObjectId("656992be9b4547ef74cfeba5"), "num" : 15 }
{ "_id" : ObjectId("656992be9b4547ef74cfeba6"), "num" : 16 }
{ "_id" : ObjectId("656992be9b4547ef74cfeba7"), "num" : 17 }
{ "_id" : ObjectId("656992be9b4547ef74cfeba8"), "num" : 18 }
{ "_id" : ObjectId("656992be9b4547ef74cfeba9"), "num" : 19 }
{ "_id" : ObjectId("656992be9b4547ef74cfebaa"), "num" : 20 }
Type "it" for more
> it
{ "_id" : ObjectId("656992be9b4547ef74cfebab"), "num" : 21 }
{ "_id" : ObjectId("656992be9b4547ef74cfebac"), "num" : 22 }
{ "_id" : ObjectId("656992be9b4547ef74cfebad"), "num" : 23 }
{ "_id" : ObjectId("656992be9b4547ef74cfebae"), "num" : 24 }
{ "_id" : ObjectId("656992be9b4547ef74cfebaf"), "num" : 25 }
{ "_id" : ObjectId("656992be9b4547ef74cfebb0"), "num" : 26 }
{ "_id" : ObjectId("656992be9b4547ef74cfebb1"), "num" : 27 }
{ "_id" : ObjectId("656992be9b4547ef74cfebb2"), "num" : 28 }
{ "_id" : ObjectId("656992be9b4547ef74cfebb3"), "num" : 29 }
21.大于40,小于50的数据:

 

 24.11-20条数据:

 

 25.21-30的数据:

db.number.find().skip(20).limit(10)

 ###################################################################

sort排序

db.a.find().sort({num:-1})

 

 复合条件:

 

 

投影:

db.a.find({},{num:1,_id:0,num:1})

 

posted @ 2023-11-30 15:05  往事已成昨天  阅读(12)  评论(0编辑  收藏  举报