MongoDB Learning Notes - Mongo Shell Common Query Commands

MongoDB Learning Notes (3) - Mongo Shell Common Query Command

The version of MongoDB used in this article is 4.0.10

> db.version();
4.0.10

1. find command for brief query

Find (query condition, field returned) 

1. Return all fields when querying

Db. user. find () - > Query all data in the user collection

> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

Db. user. find ({"username": "Mary"}) - > Lists the data for username=Mary

> db.user.find({"username": "Mary"})
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }

Db. user. find ({"age": 50}) - > Lists age = 50 data

> db.user.find( {"age":50} )
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

2. When querying, only a few fields are returned.

Db. user. find ({}, {"username": 1}) > Lists the username fields for all people

> db.user.find({}, {"username": 1})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary" }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin" }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart" }

Db. user. find ({}, {"tel": 1}) - > Lists the Tel fields for all people

> db.user.find({}, {"tel": 1})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7") }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8") }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9") }

Db. user. find ({}, {age": 0}) - > All fields except age field are listed.

> db.user.find({}, {"age": 0})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary" }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin" }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart" }

II. Common Operators

1. $lt (<)

Query users younger than 30

> db.user.find( {"age": { $lt: 30 } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }

2. $lte (<=)

Query users younger than or equal to 30 years old

> db.user.find( {"age": { $lte: 30 } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }

3. $gt (>)

Query Users Aged Over 30

> db.user.find( {"age": { $gt: 30 } } )
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

4. $gte (>= )

Query users older than or equal to 30

> db.user.find( {"age": { $gte: 30 } } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

5. $ne ( <> )

Users whose query age is not equal to 30 years old

> db.user.find( {"age": { $ne: 30 } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

6. $and (AND)

Query users older than 10 and younger than 40

> db.user.find( { $and: [ { "age": { $lt: 40 } }  , {"age": { $gt: 10 } } ] } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }

7. $or (OR)

Query users younger than 20 or older

> db.user.find( { $or: [ { "age": { $lt: 20 } }  , {"age": { $gt:40 } } ] } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

8. $in (IN)

Query Users Aged 20, 30, 40

> db.user.find( { "age" : { $in : [20, 30, 40] } } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }

9. $not (NOT)

Users not 20, 40 years old ($not)

> db.user.find( { "age" : { $not: { $in : [20, 40] } } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

10. $nin (NOT IN)

Query users aged 20, 40

> db.user.find( { "age" : { $nin : [20, 40] } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

11. $mod

Users of Query Age for 4 Modular Remaining 2

> db.user.find( {"age" : { $mod : [4, 2] } } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

12. $exists

exists is used to determine whether the key is

Add a tel field to Mary with a value of null

> db.user.update({ "username": "Mary" }, { $set : { "tel" : null } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

List records with tel fields

> db.user.find({ "tel" : { $exists : 1 }})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }

When judging null, we should pay attention to:
db.user.find({ "friends" : null })
This command not only finds records with null values, but also records that do not exist with the friends key.

> db.user.find({ "tel" : null })
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

If you want to take out the friends key that exists and has a null value, you should do so:

> db.user.find( { $and: [ { "tel" : null }, { "tel" : {$exists: 1 } } ] } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }

13. $where

where: Determine whether the data is returned based on the return value of the function

Removal age * 3 + 5 less than 100 users

db.user.find( { $where : function(){ 
    return (this.age * 3 + 5 < 100); 
} } );
> db.user.find( { $where : function(){ return (this.age * 3 + 5 < 100); } } );
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }

this represents the current record.

With this $where, you can basically implement any type of query. But don't use this method until it's necessary, because it's much slower than normal queries.

3. Use regular queries

Find a record with "art" in the name

> db.user.find( { "username" : /art/ } )
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

Find records that begin with the name "mar" and are case-insensitive

> db.user.find( {"username": /^mar/i } );
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }

Query Array

1. $all: When determining multiple specified values contained in an array type field.

Add some friends to users in the user collection, and the results are as follows:

> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086", "friend" : [ "Mary", "Jocker", "Martin", "Kart" ] }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null, "friend" : [ "Jocker", "Martin" ] }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40, "friend" : [ "Mary", "Jocker", "Kart" ] }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

Remove both Mary and Jocker records from the friend array

> db.user.find({ "friend": { $all : ["Mary", "Jocker"] } })
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086", "friend" : [ "Mary", "Jocker", "Martin", "Kart" ] }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40, "friend" : [ "Mary", "Jocker", "Kart" ] }

2. $size: Query array with specified number of elements

Remove records with three values in the friend array

> db.user.find( { "friend" : { $size  : 3 } } )
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40, "friend" : [ "Mary", "Jocker", "Kart" ] }

3. $slice: Returns a subset of an array

{$slice: 10} - > Top 10 in the array
{$slice: -10} - > the last 10 in the array
{$slice: [20, 10]} - > Start with the element subscribed to 20 in the array and remove 10 elements backwards

Take out Tom's first three friends

> db.user.find( {"username": "Tom"}, { "friend" : {$slice : 3} } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086", "friend" : [ "Mary", "Jocker", "Martin" ] }

5. Searching for Embedded Documents

1. $elemMatch

If there is such a data structure:
There is a comments field, which is an array, each of which is an embedded comment object.

{
    "_id": ObjectId("5d31b1d24fd0d7ad0a1a1361"),
    "author": "Tom",
    "content": "I am Tom!",
    "comments": [{
        "user": "Mary",
        "score": 3,
        "comment": "Nice!"
    }, {
        "user": "Martin",
        "score": 6,
        "comment": "I'm reading..."
    }, {
        "user": "Jocker",
        "score": 8,
        "comment": "You're kidding me"
    }]
}
{
    "_id": ObjectId("5d31b3114fd0d7ad0a1a1364"),
    "author": "Martin",
    "content": "I am Martin!",
    "comments": [{
        "user": "Tom",
        "score": 5,
        "comment": "Nice!"
    }, {
        "user": "Mary",
        "score": 6,
        "comment": "I'm reading..."
    }, {
        "user": "Jocker",
        "score": 3,
        "comment": "You're kidding me"
    }]
}
{
    "_id": ObjectId("5d31b3314fd0d7ad0a1a1365"),
    "author": "Mary",
    "content": "I am Mary!",
    "comments": [{
        "user": "Tom",
        "score": 3,
        "comment": "Nice!"
    }, {
        "user": "Martin",
        "score": 5,
        "comment": "I'm reading..."
    }, {
        "user": "Jocker",
        "score": 2,
        "comment": "You're kidding me"
    }]
}

Query comments include articles with score greater than 5

> db.blog.find ({ "comments" : { "$elemMatch" : { "score" : {"$gt" : 5}}} })
{ "_id" : ObjectId("5d31b1d24fd0d7ad0a1a1361"), "author" : "Tom", "content" : "I am Tom!", "comments" : [ { "user" : "Mary", "score" : 3, "comment" : "Nice!" }, { "user" : "Martin", "score" : 6, "comment" : "I'm reading..." }, { "user" : "Jocker", "score" : 8, "comment" : "You're kidding me" } ] }
{ "_id" : ObjectId("5d31b3114fd0d7ad0a1a1364"), "author" : "Martin", "content" : "I am Martin!", "comments" : [ { "user" : "Tom", "score" : 5, "comment" : "Nice!" }, { "user" : "Mary", "score" : 6, "comment" : "I'm reading..." }, { "user" : "Jocker", "score" : 3, "comment" : "You're kidding me" } ] }

Comment records with score greater than 5 in comments querying Tom's articles

> db.blog.find ({"author": "Tom"}, { "comments" : { "$elemMatch" : { "score" : {"$gt" : 3}}} })
{ "_id" : ObjectId("5d31b1d24fd0d7ad0a1a1361"), "comments" : [ { "user" : "Martin", "score" : 6, "comment" : "I'm reading..." } ] }

Keywords: Database MongoDB shell less

Added by john_zakaria on Tue, 30 Jul 2019 14:22:11 +0300