mongodb中1.新建一个数据库javamongotest 2.新建两个集合(user和owner),信息如下,要求:同时插入多个文档(insertMany) { "name" : “xiuxiu", "hobby" : "piano", “times”:6 } { "name" : chongyu", "hobby" : "basketball", “times”:5 } { "name" : "ruizhi", "hobby" : "books" “times”:3 }, { "name" :” yixin", "hobby" : " drawing" “times”:4 } { "name" : “yufeng", "hobby" : "music" “times”:6 } 2.向student集合插入如下文档 { "name" : “kefan", "hobby":”cloud computing”, times:1 } 3.统计文档user集合中的文档数量 4.查询name为kefan的文档,添加一个hobby属性 "likes":”helping others” 5.使用{name:"科凡"} 替换 name 为kefan 的文档 6.查询times>=5的user信息,并删除
时间: 2023-07-03 12:04:47 浏览: 68
1.新建一个数据库javamongotest并新建两个集合(user和owner),并同时插入多个文档(insertMany)
```
// 连接数据库
conn = new Mongo();
db = conn.getDB("javamongotest");
// 新建集合user并插入多个文档
db.user.insertMany([
{
"name": "xiuxiu",
"hobby": "piano",
"times": 6
},
{
"name": "chongyu",
"hobby": "basketball",
"times": 5
},
{
"name": "ruizhi",
"hobby": "books",
"times": 3
},
{
"name": "yixin",
"hobby": "drawing",
"times": 4
},
{
"name": "yufeng",
"hobby": "music",
"times": 6
}
]);
// 新建集合owner并插入多个文档
db.owner.insertMany([
{
"name": "jack",
"age": 28,
"phone": "123456789"
},
{
"name": "tom",
"age": 35,
"phone": "987654321"
},
{
"name": "lucy",
"age": 25,
"phone": "135792468"
}
]);
```
2.向student集合插入如下文档
```
db.student.insertOne({
"name": "kefan",
"hobby": "cloud computing",
"times": 1
});
```
3.统计文档user集合中的文档数量
```
db.user.count();
```
4.查询name为kefan的文档,添加一个hobby属性"likes":”helping others”
```
db.student.updateOne(
{ "name": "kefan" },
{ $set: { "likes": "helping others" } }
);
```
5.使用{name:"科凡"} 替换 name 为kefan 的文档
```
db.student.replaceOne(
{ "name": "kefan" },
{ "name": "科凡", "hobby": "cloud computing", "times": 1 }
);
```
6.查询times>=5的user信息,并删除
```
db.user.find({ "times": { $gte: 5 } }).forEach(function(doc) {
db.user.deleteOne({ "_id": doc._id });
});
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)