MongoDB Demo {_id: 1,title: "Hello Tom",text: "One day, I meet Kate. We have dinner in KFC.",author_id: 15,author_name: "Tom",votes: 120,gender: "M", tags: ["food", "Tom", "KFC"]}Please implement the statements to:(1)Show the title and text
{
_id: 1,
title: "Hello Tom",
text: "One day, I meet Kate. We have dinner in KFC.",
author_id: 15,
author_name: "Tom",
votes: 120,
gender: "M",
tags: ["food", "Tom", "KFC"]
}
Please implement the statements to:
(1) Show the title and text of all Tom's articles.
db.ch2_curd_test.find(
{author_name:'Tom'},
{title:1, text:1}
)
(2) Show the title and author_name of the article which has the highest votes.
db.ch2_curd_test.find({},{title :1, author_name :1}).sort({votes:-1}).limit(1)
(3) Show the entire information of those articles with votes >= 100 and votes < 500.
db.ch2_curd_test.find(
{
'votes':{$gte:100,$lt:500}
}
)
(4) Show the entire information of those articles contain 'food' tag & 'study' tag.
db.ch2_curd_test.find(
{
$and:[
{tags:'food'}, {tags:'study'}
]
}
)
(5) Tom wants to change his gender from Male to Female, please help to update the data.
db.ch2_curd_test.updateOne(
{'author_name':'Tom'},
{
$set: {'gender':'F'}
}
)
(6) Mary wants to add a tag "IT" to her article "I study Java", please help to update the data.
db.ch2_curd_test.updateMany(
{'author_name':'Mary','title':'I study Java'},
{
$push:{'tags':'IT'}
}
)
