Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize Field judgment in MongoDB

Shulou Source: shulou.com Published: 2022-05-31 21:13:49 10月03日 Update

Today, I will talk to you about how to achieve field judgment in MongoDB, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Use "$exists" to determine whether a field exists, if set to true means it exists, and if it does not exist along with false.

Example: query information about parents personnel

> db.emp.find ({"parents": {"$exists": true}}) .pretty ()

{

"_ id": ObjectId ("599148bd0184ff511bf02b91")

"name": "Lin A"

"sex": "male"

"age": 22

"sal": 8000

"loc": "Beijing"

"course": [

"Chinese"

"Mathematics"

"English"

"Music"

"politics"

]

"parents": [

{

"name": "Lin A Father"

"age": 50

Job: the Peasant

}

{

"name": "Lin A Mother"

"age": 49

"job": "worker

}

]

}

Example: query information that does not have course personnel

> db.emp.find ({"course": {"$exists": false}}) .pretty ()

{

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

"name": "Zhao Yi"

"sex": "male"

"age": 30

"sal": 1000

"loc": "Beijing"

}

You can use such queries to filter unwanted data. It is strongly recommended that the composition of the data be consistent.

After reading the above, do you have any further understanding of how to implement field judgment in MongoDB? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Fields content queries people information data examples Beijing consensus farmers workers suggestions politics mathematics more mother father knowledge articles industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn MariaDB Redmi NVidia