当前位置:网站首页>Mongoose condition queries part of the data of the specified attribute
Mongoose condition queries part of the data of the specified attribute
2022-07-28 20:37:00 【Xiao language is scattered】
Mongoose Conditionally query part of the data of the specified attribute
I encountered a problem of data query when I was rolling out the code today , How to retrieve some specified values of an attribute in a database table , for example , Data sheet stable There is an attribute in it, which is status, Its value may be a,b,c,d, Now I just need to retrieve all the items in the table status The value of is a,b The data of , How to achieve ?
Previously, I wrote the function of retrieving some specified attributes in the database , Here is how to realize this requirement , Use mongoose Of model Of select Method , You can retrieve the required data , There are two ways of writing :
- model.select(“item1 item2 item3”);
stay select The attribute name to be retrieved is passed in the method , Just separate it with a space , This kind of writing is more direct , If you need to retrieve item1 Child attributes of a, You can write directly item1.a That's all right. , Such as model.select(“item1.a item2 item3”);
- model.select({item: 1, item2: 1, item: 0});
stay select Method to pass in an object ,1 Represents that this attribute needs to be retrieved ,0 Representing this attribute does not need to be retrieved , Properties that do not need to be retrieved do not need to be written .
These two ways of writing can be achieved , Depending on personal preference .
So how to realize the problem of the beginning of the article ,mongoose There is an interesting way to write ,“$”, It can realize some interesting functions .
$ in Can solve this problem , It means that one key corresponds to multiple values , for example model.find({status: {$in:[a,b]}}); What it does is retrieve all status The value of is a,b The data of .
$ nin We can also solve this problem , It means that a key does not correspond to the specified value , for example model.find({status: {$nin:[c,d]}}); It implements the retrieval of all status The value of the c,d The data of .
The two writing methods are applicable to different scenes , Their realizable functions are also different .
I also sorted out some other ways of writing , You can have a look if you need .
$lt Less than a certain value {age: {$lt: 30}}
$lte Less than or equal to a certain value {age: {$lte: 30}}
$gt Greater than a certain value {age: {$gt: 30}}
$gte Greater than or equal to {age: {$gte: 30}}
$eq be equal to {age: {$eq: 30}}
$ne It's not equal to {age: { $ne:24}}
$in A key corresponds to multiple values {age: {$in:[20,30]}}
$nin A key does not correspond to the specified value {age: {$nin:[20,30]}}
$or Multiple condition matching , Can be nested $in Use {$or: [{name: "name"}, {age: 28}]}
$nor Query documents that do not match a particular pattern
$exists Determine whether some key fields exist {name: {$exists: true}}
边栏推荐
- [C language] use function pointers to make a different calculator
- [pytorch] LSTM neural network
- Simple use of robobrowser
- C# 委托 delegate 的理解
- Use of DDR3 (axi4) in Xilinx vivado (4) incentive design
- User and group and authority management
- MySQL startup error 1607 unexpected process termination
- 83. (cesium home) how the cesium example works
- Raspberry pie 4B ffmpeg RTMP streaming
- [POC - proof of concept]
猜你喜欢

Leetcode-297 serialization and deserialization of binary tree

js飞入js特效弹窗登录框

弹出模态框

Windows系统下Mysql数据库定时备份

Pop up modal box
![Maximum exchange [greedy thought & monotonic stack implementation]](/img/ad/8f0914f23648f37e1d1ce69086fd2e.png)
Maximum exchange [greedy thought & monotonic stack implementation]

The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution

Use of DDR3 (axi4) in Xilinx vivado (1) create an IP core

C语言数据 3(2)

Voice controlled robot based on ROS (II): implementation of upper computer
随机推荐
Raspberry pie 3b ffmpeg RTMP streaming
关于链接到其他页面的标题
Introduction to seven kinds of polling (practice link attached)
太空射击第10课: Score (繪畫和文字)
The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution
七种轮询介绍(后附实践链接)
Related concepts of multitasking programming
Network shell
Common instructions of vim software
PXE_ KS unattended system
js图表散点图例子
Solve the brick stacking problem (DP)
Read JSON configuration file to realize data-driven testing
User, user group related operations
Quick sort template
Wildcard ssl/tls certificate
Method number problem for solving sum of numbers (knapsack problem)
Linxu 【权限,粘滞位】
类与对象(中)
Solutions to the environment created by Anaconda that cannot be displayed in pycharm