Modifier Enhancement for Conquering Mongodb

Posted by sois on Mon, 09 Sep 2019 07:19:28 +0200

Links to the original text: https://my.oschina.net/mohaiyong/blog/221296

Simple queries were made through find/find One, and it seemed too simple to find out in a trance what was missing. Here's a supplement. Let's briefly talk about the keywords of all kinds of dollars: $lt, $gt, $lte, $gte, and so on.

I found myself digging another pit, deep and wide, and needed to jump out early to catch my breath.

 

 

Cluster configuration related links:

Conquering Mongodb Installation and System Service Configuration

Master-slave Replication Conquering Mongodb: Cluster Replication

 

Basic operations related links:

Common commands and basic data types for conquering Mongodb  

Modifier's Initial Knowledge of Conquering Mongodb

Modifier Enhancement for Conquering Mongodb

Conquering CRUD of Mongodb

It seems to me that this book (MongoDB Authoritative Guide) has been bypassed, and if it is split up like this, it is easy to regard these MongoDB Modifier s as unique parts only to Update.

 

I. OR

  •  $or

For queries about the "and" relationship, see the "CRUD" article for more details. Here we just talk about OR queries.

 

> db.user.findOne({"$or" :[{"age" : 30},{"uid" : "u1234567890"}]})
{
        "_class" : "org.zlex.mongodb.domain.User",
        "_id" : ObjectId("50fe4f627252799620eee0db"),
        "address" : "Shanghai",
        "age" : 30,
        "email" : [
                "snowolf@sina.com",
                "snowolf@sohu.com",
                "snowolf@yahoo.com",
                "snowolf@baidu.com"
        ],
        "uid" : "u1234567890"
}

The {age": 30}, {uid": `u1234567890'} satisfies one of the conditions.

 

    

  • $not
> db.user.findOne({"age" : {"$not":{"$gt":40}}})
{
        "_class" : "org.zlex.mongodb.domain.User",
        "_id" : ObjectId("50fe4f627252799620eee0db"),
        "address" : "Shanghai",
        "age" : 30,
        "email" : [
                "snowolf@sina.com",
                "snowolf@sohu.com",
                "snowolf@yahoo.com",
                "snowolf@baidu.com"
        ],
        "uid" : "u1234567890"
}

Data retrieval with age no more than 40

 

2. Scope

  • $lt,$gt,$lte,$gte

This refers to the special uses of weird identifiers like $lt, $gt, $lte, $gte. In fact, as long as you are familiar with the XML format, these abbreviations should not be unfamiliar. The corresponding mathematical symbols of $lt, $gt, $lte, $gte are <, >, <=, >=.

 

> db.user.findOne({"age" : {"$gte":20,"$lte":50}})
{
        "_class" : "org.zlex.mongodb.domain.User",
        "_id" : ObjectId("50fe4f627252799620eee0db"),
        "address" : "Shanghai",
        "age" : 30,
        "email" : [
                "snowolf@sina.com",
                "snowolf@sohu.com",
                "snowolf@yahoo.com",
                "snowolf@baidu.com"
        ],
        "uid" : "u1234567890"
}

 

 

Equivalent to: where age >= 20 and age <= 50

 

  • $in,$nin

If you want to query multiple values of a key, you can use $in

 

> db.user.findOne({"email" : {"$in":["snowolf@sina.com","snowolf@baidu.com"]}})
{
        "_class" : "org.zlex.mongodb.domain.User",
        "_id" : ObjectId("50fe4f627252799620eee0db"),
        "address" : "Shanghai",
        "age" : 30,
        "email" : [
                "snowolf@sina.com",
                "snowolf@sohu.com",
                "snowolf@yahoo.com",
                "snowolf@baidu.com"
        ],
        "uid" : "u1234567890"
}

On the contrary, $nin

 

 

> db.user.findOne({"email" : {"$nin":["snowolf@163.com","snowolf@qq.com"]}})
{
        "_class" : "org.zlex.mongodb.domain.User",
        "_id" : ObjectId("50fe4f627252799620eee0db"),
        "address" : "Shanghai",
        "age" : 30,
        "email" : [
                "snowolf@sina.com",
                "snowolf@sohu.com",
                "snowolf@yahoo.com",
                "snowolf@baidu.com"
        ],
        "uid" : "u1234567890"
}

  

OK, the assault mission was basically completed.

For Modifier identifiers, see http://cn.docs.mongodb.org/manual/reference/operator/

 

 

 

 

Cluster configuration related links:

Conquering Mongodb Installation and System Service Configuration

Master-slave Replication Conquering Mongodb: Cluster Replication

 

Basic operations related links:

Common commands and basic data types for conquering Mongodb  

Modifier's Initial Knowledge of Conquering Mongodb

Modifier Enhancement for Conquering Mongodb

Conquering CRUD of Mongodb

Reproduced in: https://my.oschina.net/mohaiyong/blog/221296

Topics: MongoDB xml