当前位置 : 主页 > 网络编程 > JavaScript >

MongoDB Demo

来源:互联网 收集:自由互联 发布时间:2021-06-30
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
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 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'}
        }
    )
上一篇:rem布局产生的bug
下一篇:angular-service.js
网友评论