mongodb操作文档

文章目录

概要

mongodb操作文档
版本 v6.0.11

题目

2.(6) 在test数据库order集合中插入以下订单信息,输出"zhangsan"的订单信息,查询结果使用pretty()格式化显示。

{   _id:2,

   name:"zhangsan",

   title:"商品购物单5",

   amount:60,

   unit:"元",

   detail:[  {name:"苹果",price:22},

                {name:"面粉",price:18}

              ],

   Date:"2017-16-30 12:05:06",

   overview:{shop:"丁丁商务平台",address:"地球村"}

}

(7) 将"zhangsan"的订单细则detail 买的第二种物品改为 {name:“大米”,price:40},购买地址address修改为"天津市和平区成都道9号"。pretty()查询显示修改后的"zhangsan"订单信息。

将"zhangsan"订单细则中的水果—— 苹果 改为“梨” ,查询显示修改后的"zhangsan"订单信息。

(8) 使用update方法新增"lisi"订单细则 ,信息包括_id:10,{name:“西瓜”,price:10},unit:“元” ,查询显示"lisi"订单信息。

3.读取文件:load(“bookshop.js”);以下题目查询结果均使用pretty()格式化显示信息。

 db.bookshop.insert( {
	"_id":"185.3.16",
	"book": {
		"callnum":"185.3.16",
		"isbn":"1-292-06118-9",
		"title":"Database Systems",
		"authors":[
			{
				"fname":"Thomas",
				"lname":"Connolly"},
			{ 
				"fname":"Carolyn",
				"lname":"Begg"}
		],
		"publisher":"Pearson Pty Ltd",
		"year":2015,
		"price":136.99,
		"topic":"Computer Science",
		"description":"This is the 6th edition. You can register online to access the examples",
		"keywords":["Database", "XML", "Distributed"]
		}
});
		
db.bookshop.insert( {
	"_id":"163.24.12",
	"book": {
		"callnum":"163.24.12",
		"isbn":"1-123-456-810",
		"title":"Core Java",
		"authors":[
			{
				"fname":"Horstmann",
				"lname":"Cornell"}
		],
		"publisher":"PH Pty Ltd",
		"year":2012,
		"price":142.90,
		"topic":"Computer Science",
		"description":"It covers JAVA programming and JAVA script",
		"keywords":["JAVA", "XML", "Script"]
	}
});

db.bookshop.insert( {
	"_id":"123.45.67",
	"book": {
		"callnum":"123.45.67",
		"isbn":"1-123-456-789",
		"title":"Algorithms",
		"authors":[
			{
				"fname":"James",
				"lname":"Bond"},
			{
				"fname":"Harry",
				"lname":"Potter"},
			{
				"fname":"William",
				"lname":"Stallings"}
		],
		"publisher":"Pearson Pty Ltd",
		"year":2013,
		"price":65.85,
		"topic":"Computer Science",
		"description":"It contains algorithms and their applications. You can download examples from the website"
	}
});
db.bookshop.insert( {
	"_id":"134.41.33",
	"book": {
		"callnum":"134.41.33",
		"isbn":"1-213-431-770",
		"title":"C++ Programming",
		"authors":[
			{
				"fname":"Larry",
				"lname":"Peterson"}
		],
		"publisher":"Pearson Pty Ltd",
		"year":2010,
		"price":74.90,
		"topic":"Computer Science",
		"description":"C++ programming and its applications",
		"keywords":["C++", "Class", "Overloading", "Inheritance"]
	}
});

db.bookshop.insert( {
	"_id":"214.56.82 12.23",
	"journal": {
		"callnum":"214.56.82",
		"issn":"S-11-123-123-456",
		"title":"Handyman",
		"vol":12,
		"issue":23,
		"price":9.85,
		"year":2017,
		"month":"February"
	}
});

db.bookshop.insert( {
	"_id":"214.56.82 12.24",
	"journal": {
		"callnum":"214.56.82",
		"issn":"S-11-123-123-456",
		"title":"Handyman",
		"vol":12,
		"issue":24,
		"price":9.85,
		"year":2017,
		"month":"August"
	}
});


db.bookshop.insert( {
	"_id":"214.56.82 12.25",
	"journal": {
		"callnum":"214.56.82",
		"issn":"S-11-123-123-456",
		"title":"Handyman",
		"vol":12,
		"issue":25,
		"price":9.85,
		"year":2017,
		"month":"October"
	}
});

db.bookshop.insert( {
	"_id":"263.18.37 5.33",
	"journal": {
		"callnum":"263.18.37",
		"issn":"D-10-123-124-456",
		"title":"Information processing",
		"vol":5,
		"issue":33,
		"price":15.90,
		"year":2018,
		"month":"January"
	}
});

db.bookshop.insert( {
	"_id":"263.18.37 5.34",
	"journal": {
		"callnum":"263.18.37",
		"issn":"D-10-123-124-456",
		"title":"Information processing",
		"vol":5,
		"issue":34,
		"price":15.90,
		"year":2018,
		"month":"May"
	}
});

db.bookshop.insert( {
	"_id":"285.32.74 17.65",
	"journal": {
		"callnum":"285.32.74",
		"issn":"C-9-123-456-666",
		"title":"Mathmetics and Computing",
		"vol":17,
		"issue":65,
		"price":35.90,
		"year":2018,
		"month":"December"
	}
});

db.bookshop.insert( {
	"_id":"285.32.74 17.66",
	"journal": {
		"callnum":"285.32.74",
		"issn":"C-9-123-456-666",
		"title":"Mathmetics and Computing",
		"vol":17,
		"issue":66,
		"price":35.90,
		"year":2019,
		"month":"February"
	}
});

db.bookshop.insert( {
	"_id":"321.12.76",
	"musicCD": {
		"callnum":"321.12.76",
		"title":"Music",
		"producer":"ABC Pty Ltd",
		"year":2010,
		"category":"Pop",
		"price":12.80
	}
});

db.bookshop.insert( {
	"_id":"342.22.42",
	"musicCD": {
		"callnum":"342.22.42",
		"title":"Stars",
		"producer":"BBC Pty Ltd",
		"year":2012,
		"category":"Classic",
		"price":12.80
	}
});

db.bookshop.insert( {
	"_id":"332.17.25",
	"musicCD": {
		"callnum":"332.17.25",
		"title":"Wiggle Wiggle",
		"producer":"Wiggle Pty Ltd",
		"year":2005,
		"category":"Kids",
		"price":10.50
	}
});

db.bookshop.insert( {
	"_id":"402.12.1",
	"magazine": {
		"callnum":"402.12.1",
		"title":"ORACLE",
		"publisher":"Julia McVeigh",
		"topic":"Database",
		"price":4.50,
		"year":2017,
		"month":"June",
		"contents":[
			{"event":[{"current":{"page":14}}, {"upcoming":{"page":15}}]},
			{"columns":[
				{"title":"Getting good service", "page":17, "author":"Jeff Spicer"},
				{"title":"Behind Unbreakable", "page":19, "author":"Ken Jacobs"},
				{"title":"Getting good", "page":21, "author":"Rich Niemiec"},
				{"title":"Building and using portals", "page":25, "author":"Robert Hall"},
				{"title":"Blocks, messages, rows", "page":29, "author":"Tom Kyte"}
			]},
			{"upfront":{
				"Oracle monitor":{
					"page":39,
					"titles":["JDeveloper arrives", "Oracle and network", "Supply chain goes with flow"]
				}, 
				"Did you know":{"page":40}, 
				"Share pool":{
					"page":53,
					"titles":["Systems expands content-management", "ArtinSoft automates migration to Java"]
				}
			} }
		]
	}
});

db.bookshop.insert( {
	"_id":"402.12.2",
	"magazine": {
		"callnum":"402.12.2",
		"title":"ORACLE",
		"publisher":"Julia McVeigh",
		"topic":"Database",
		"price":4.50,
		"year":2019,
		"month":"July",
		"contents":[
			{"event":[{"current":{"page":9}}, {"upcoming":{"page":10}}]},
			{"columns":[
				{"title":"Making integration", "page":15, "author":"Jeff Spicer"},
				{"title":"Oracle retrospective", "page":21, "author":"Rich Niemiec"},
				{"title":"Stretching the Web", "page":27, "author":"Robert Hall"},
				{"title":"Beyond Init.ora and SYS", "page":29, "author":"Tom Kyte"}
			]},
			{"upfront":{
				"Oracle monitor":{
					"page":39,
					"titles":["Oracle information architecture", "Certifications debut at OpenWorld", "Big new for small business", "V-business on the horizon"]
				}, 
				"Did you know":{"page":40}, 
				"Share pool":{
					"page":47,
					"titles":["Luminate. Net intelligent database management", "Unicenter supports Oracle", "SDA's Web database objects"]
				}
			} }
		]
	}
});




(9) 在bookshop中插入一本新书的信息,显示该图书信息,查询结果使用pretty()格式化显示新书信息。

{     "_id": "6666666",
     "book":{
                       "isbn":"6-666-666-666",
                        "title":"NoSQL数据库入门与实践",
                       "authors":[  {

                                           "first":"刘瑜","

                                            second":"刘胜松"}
                                       ],
                       "publisher":"中国水利水电出版社",
                        "year":2018,
                        "price":89.80,
                        "topic":"计算机科学",
                         "description":"NoSQL初学者了解NoSQL技术全貌的图书"
}

(10) 在"Database Systems"该书的文档中插入两个新的(keywords)关键字PL/SQL和NoSQL,显示图书标题和关键字。

(11) 查询isbn编号为1-123-456-810的图书信息; 为isbn编号为1-123-456-810的书插入一个新的作家{“fname”:“Harry”,“lname”:" Potter"}; 查询显示书籍的isbn,标题和作者的信息;

(12) 查询显示isbn编号为1-123-456-810该本图书的isbn,标题和第一作者的信息。
(13) 查询书名叫做"Database Systems"的图书价格;把书名叫做"Database Systems"的图书价格降低20%,查询结果显示书名和价格。
(14) 查询图书信息中存在关键字year的文档,显示所有书籍的标题,出版商和出版年份。
将图书中的关键字year重命名为publication year,显示所有书籍的标题,出版商和出版年份。
(15) 对于标题title为Algorithms 且价格为65.85的一本书,将主题topic的值更改为Information Technology,显示书的isbn,标题,价格和主题。

(16) 查询callnum为123.45.67的图书名称,作者信息;从callnum为123.45.67的图书信息中删除作者{“fname”:“James”,“lname”:“Bond”},显示书名和作者。

(17) 从数据库中删除"title"为"NoSQL数据库入门与实践"这本图书有关的信息。查询删除结果——只显示所有图书信息,不包括杂志等其他信息。

(18) 从数据库中删除isbn为1-213-431-770的一本书的description描述信息,显示删除后图书信息。

(19) 显示所有杂志magazine的信息;显示数据库中描述杂志magazine的文档数量。

(20) 从数据库中删除所有杂志magazine的信息,显示数据库中还剩下多少描述杂志的文档。

参考

提示:仅供参考(有错误麻烦指出)操作中省略了部分查询操作

(6)

 db.order.insert({   _id:2,
... 
...    name:"zhangsan",
... 
...    title:"商品购物单5",
... 
...    amount:60,
... 
...    unit:"元",
... 
...    detail:[  {name:"苹果",price:22},
... 
...                 {name:"面粉",price:18}
... 
...               ],
... 
...    Date:"2017-16-30 12:05:06",
... 
...    overview:{shop:"丁丁商务平台",address:"地球村"}
... 
... })

(7)

db.order.updateOne({ name: "zhangsan" }, 
...     {
...       $set: {
...         "detail.1": { name: "大米", price: 40 }, 
...         "overview.address": "天津市和平区成都道9号" 
...       }
...     })

db.order.updateOne(
    { name: "zhangsan", "detail.name": "苹果" }, 
    { $set: { "detail.$.name": "梨" } }

)

(8)

db.order.updateOne(
...     { name: "lisi" },
...     {
...       $set: {
...         "_id": 10,
...         "detail": [{ name: "西瓜", price: 10 }],
...         "unit": "元"
...       }
...     },{upsert:true})

(9)

db.bookshop.insertOne({
...     "_id": "6666666",
...     "book": {
...         "isbn": "6-666-666-666",
...         "title": "NoSQL数据库入门与实践",
...         "authors": [
...             {
...                 "first": "刘瑜",
...                 "second": "刘胜松"
...             }
...         ],
...         "publisher": "中国水利水电出版社",
...         "year": 2018,
...         "price": 89.8,
...         "topic": "计算机科学",
...         "description": "NoSQL初学者了解NoSQL技术全貌的图书"
...     }
... })

(10)

db.bookshop.updateOne(
...     {"book.title":"Database Systems"},
...     {
...         $push:{
...             "book.keywords":"PL/SQL",
...             "book.keywords": "NoSQL"
...         }
...     }
... )

db.bookshop.findOne(
...      { "book.title": "Database Systems" },
...      { "book.title": 1, "book.keywords": 1 },
... 
... )

(11)

db.bookshop.updateOne(
... { "book.isbn": "1-123-456-810" },
...     {
...       $push: {
...               "book.authors": { "fname": "Harry", "lname": "Potter" }
...             }     
...       }
... 
... )
db.bookshop.findOne({"book.isbn": "1-123-456-810"},{ "book.isbn": 1, "book.title": 1, "book.authors": 1 })

(12)

db.bookshop.findOne(
...     {
...       "book.isbn": "1-123-456-810"
...     },
...     {
...       "book.isbn": 1,
...       "book.title": 1,
...       "book.authors": { $slice: 1 } 
...     }
... 
... )

(13)

这里直接乘数据会NAN
db.bookshop.updateOne({ "book.title": "Database Systems" },{$set:{"book.price":109.59}})

(14)

 db.bookshop.find({
...     $or: [
...       { "book.title": /year/i },
...       { "book.publisher": /year/i },
...       { "book.year": { $type: "number" } }
...     ]
...   },
...      {
...        "_id": 0,
...       "book.title": 1,
...       "book.publisher": 1,
...       "book.year": 1
...     }
...   )

db.bookshop.updateMany(
...     {
...       $or: [
...         { "book.title": /year/i }, 
...         { "book.publisher": /year/i }, 
...         { "book.year": { $type: "number" } } 
...       ]
...     },
...     {
...       $rename: { "book.year": "book.publication year" }
...     }
...   )  

(15)

db.bookshop.updateOne(
...     {
...       "book.title": "Algorithms",
...       "book.price": 65.85
...     },
...     {
...       $set: {
...         "book.topic": "Information Technology"
...       }
...     }
...     
... )


db.bookshop.findOne(
... 
...     {
...        "book.title": "Algorithms",
...         "book.price": 65.85
...       },
...       {
...         "book.isbn": 1,
...         "book.title": 1,
...         "book.price": 1,
...         "book.topic": 1
...       }
... 
... )

(16)

db.bookshop.updateOne(
    {
    "_id": "123.45.67"
    },
    { $pull: { "book.authors": { "fname": "James", "lname": "Bond" } } }

)
db.bookshop.findOne( { "_id": "123.45.67" }, { "book.title": 1, "book.authors": 1 } )

(17)

db.bookshop.deleteOne(
    {
    "book.title": "NoSQL数据库入门与实践"
    }
)

db.bookshop.find({ "book": { $exists: true } })

(18)

db.bookshop.updateOne(
...     {
...       "book.isbn": "1-213-431-770"
...     },
...     {
...       $unset: {
...         "book.description": 1
...       }
...     }
... )

db.bookshop.findOne(
...     {
...       "book.isbn": "1-213-431-770"
...     }
... 
... )

(19)

db.bookshop.find(
    { "magazine": { $exists: true } }
)
db.bookshop.countDocuments(
    { "magazine": { $exists: true } }
)

(20)

db.bookshop.deleteMany(
    { "magazine": { $exists: true }}

)
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值