Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize logical Operation in MongoDB

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about how to achieve logic operations in MongoDB, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

There are three types of logical operation attention: and ($and), or ($or), and not ($not,$nor).

Example: query information about people aged between 20 and 30

> db.emp.find ({"age": {"$gte": 20, "$lte": 30}}) .pretty ()

{

"_ id": ObjectId ("599108423268c8e84253be26")

"name": "Zhao Yi"

"sex": "male"

"age": 30

"sal": 1000

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be27")

"name": "Qian er"

"sex": "female"

"age": 22

"sal": 5000

"loc": "Shanghai"

}

{

"_ id": ObjectId ("599108423268c8e84253be29")

"name": "Li Si"

"sex": "female"

"age": 30

"sal": 7000

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be2a")

"name": "Friday"

"sex": "female"

"age": 30

"sal": 6400

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be2b")

"name": "Wu Liu"

"sex": "male"

"age": 30

"sal": 2500

"loc": "Chongqing"

When performing logical operations, the connection of "and" is the easiest, because you only need to use "," to separate several conditions.

Example: query people who are not 30 years old

> db.emp.find ({"age": {"$ne": 30}}) .pretty ()

{

"_ id": ObjectId ("599108423268c8e84253be27")

"name": "Qian er"

"sex": "female"

"age": 22

"sal": 5000

"loc": "Shanghai"

}

{

"_ id": ObjectId ("599108423268c8e84253be28")

"name": "Sun San"

"sex": "male"

"age": 40

"sal": 2000

"loc": "Shenzhen"

}

{

"_ id": ObjectId ("599108423268c8e84253be2c")

"name": "Zheng Qi"

"sex": "female"

"age": 50

"sal": 4700

"loc": "Chengdu"

}

{

"_ id": ObjectId ("599108433268c8e84253be2d")

"name": "bastards"

"sex": "male"

"age": 35

"sal": 8000

"loc": "Beijing"

}

Example: query information about people who are older than 30 years old or whose salary is more than 5000

> db.emp.find ({"$or": [{"age": {"gt": 30}}, {"sal": {"$gt": 5000}}]}) .pretty ()

{

"_ id": ObjectId ("599108423268c8e84253be29")

"name": "Li Si"

"sex": "female"

"age": 30

"sal": 7000

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be2a")

"name": "Friday"

"sex": "female"

"age": 30

"sal": 6400

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108433268c8e84253be2d")

"name": "bastards"

"sex": "male"

"age": 35

"sal": 8000

"loc": "Beijing"

Example: reverse operation of or

> db.emp.find ({"$nor": [{"age": {"gt": 30}}, {"sal": {"$gt": 5000}}]}) .pretty ()

{

"_ id": ObjectId ("599108423268c8e84253be26")

"name": "Zhao Yi"

"sex": "male"

"age": 30

"sal": 1000

"loc": "Beijing"

}

{

"_ id": ObjectId ("599108423268c8e84253be27")

"name": "Qian er"

"sex": "female"

"age": 22

"sal": 5000

"loc": "Shanghai"

}

{

"_ id": ObjectId ("599108423268c8e84253be28")

"name": "Sun San"

"sex": "male"

"age": 40

"sal": 2000

"loc": "Shenzhen"

}

{

"_ id": ObjectId ("599108423268c8e84253be2b")

"name": "Wu Liu"

"sex": "male"

"age": 30

"sal": 2500

"loc": "Chongqing"

}

{

"_ id": ObjectId ("599108423268c8e84253be2c")

"name": "Zheng Qi"

"sex": "female"

"age": 50

"sal": 4700

"loc": "Chengdu"

}

The operation of or can achieve a function of finding the opposite.

Among these logical operations, the connection with or is the easiest, and the connection to or needs to set the filter conditions of the array for the data.

The above is how to achieve logical operations in MongoDB. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 255

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report