MongoDB操作手册CRUD查询
查询操作 基本查询 查询指定集合中的所有记录 db.testData.find()或者db.testData.find({}); 相等条件查询 db.testData.find({num:5});//查询num=5的记录 使用查询操作符声明多个条件 db.testData.find({num:{$in:[2,3,4]}});查询num为2,3,4的记录。 尽管可
查询操作
基本查询
查询指定集合中的所有记录
db.testData.find()或者db.testData.find({});相等条件查询
db.testData.find({num:5});//查询num=5的记录使用查询操作符声明多个条件
db.testData.find({num:{$in:[2,3,4]}});查询num为2,3,4的记录。尽管可以使用$or操作符来执行同样的查询,但是当多个or条件都是在同一字段上的相等判断时,请使用$in而不是$or。
and条件查询
db.testData.find({name:'d',num:{$lt:10}});//查询name为d且num小于10的记录。lt = less thanor条件查询
db.find({$or:[{name:'a'},{num:{$gt:3}}])//查询name为a或者num大于3的记录。gt = greater thanand or 混合查询
db.testData.find({gender:'male',$or:[{name:'a'},{age:{$gt:3}}]});//查询gender为male,且(名字为a或者age>3)的记录。
嵌入型记录查询
当一个字段内嵌了一条记录,查询时既可以声明精确的(即字段顺序,字段值都匹配)去匹配内嵌记录某些字段,或者使用“.”来匹配内嵌记录的各个字段声明。测试数据
db.testData.insert( { producer:{company:'a',address:'123'},name:'aaa' } );db.testData.insert( { producer:{company:'a',address:'123'} } );
精确匹配内嵌记录的某字段
原来的db.testData.find(例:db.testData.find( { producer:{company:'a',address:'123'} } );
结果将返回以上两条测试数据
注意:
db.testData.find( { producer:{address:'123',company:'a'} } )
db.testData.find( { producer:{address:'123',name:'aaa'} } )
db.testData.find( { producer:{company:'a',name:'aaa'} } )
这三条语句都不会查询到结果,即这种查询方式必须从是从第一到第n个字段的匹配,字段顺序颠倒,或者不是挨着的都不会查到结果。
匹配某些内嵌字段
db.testData.find( { 'producer.company':'a' } );db.testData.find( { 'producer.company':'a','producer.address':'123' } );
以上两条可以查询到所有测试数据。
注意:
db.testData.find( { 'producer.company':'a','producer.name':'aaa' } );
db.testData.find( { 'producer.address':'123','producer.name':'aaa' } );
db.testData.find( { 'producer.name':'aaa' } );
这三条语句也无法查询到测试数据,也就是说这种方式也必须是从第一到第n个字段的匹配,字段顺序,或者不是挨着的字段都不会查到结果
数组
概述
当某个字段保存数组,可以根据数组中的某个值来查询这条记录。如果这个数组保存着嵌入型记录,可以使用“.”查询。如果使用$elemMatch声明多个条件来进行查询,数组必须至少包含一条记录满足所有条件。
如果没有使用$elemMatch来声明多个条件进行查询,那么满足条件的记录符合的条件是:这个数组中的各个元素能够覆盖条件的每个要求,如数组5,6,7,条件为=5且=7的情况下,数组中的5和7两个元素组合起来能够覆盖所有条件要求。
测试数据
db.testData.insert({ _id: 5, type: "food", item: "aaa", ratings: [ 5, 8, 9 ] });db.testData.insert({ _id: 6, type: "food", item: "bbb", ratings: [ 5, 9 ] });
db.testData.insert({ _id: 7, type: "food", item: "ccc", ratings: [ 9, 5, 8 ] });
精确匹配数组
查询结果需要精确匹配数组中的每个值,包括每个元素的顺序
db.testData.find( { ratings: [ 5, 8, 9 ] } )匹配数组中的一个值
db.testData.find( { ratings: 5 } )匹配数组指定位置的值
db.testData.find( { 'ratings.0': 5 } )//匹配ratings数组中第一个元素为5的记录数组的多条件查询
单元素条件查询
db.testData.find( { ratings: { $elemMatch: { $gt: 5, $lt: 9 } } } )//匹配ratings数组中至少有一个元素既大于5又小于9的记录。多元素联合匹配查询
db.testData.find( { ratings: { $gt: 5, $lt: 9 } } )//匹配数组中有一个元素大于5,同时有另一个元素小于9的记录,或者某个元素同时满足这两个条件的记录。嵌入型记录数组
测试数据
{_id: 100,
type: "food",
item: "xyz",
qty: 25,
3.3. MongoDB CRUD Tutorials 91
MongoDB Documentation, Release 2.6.4
price: 2.5,
ratings: [ 5, 8, 9 ],
memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}
{
_id: 101,
type: "fruit",
item: "jkl",
qty: 10,
price: 4.25,
ratings: [ 5, 9 ],
memos: [ { memo: "on time", by: "payment" }, { memo: "delayed", by: "shipping" } ]
}
使用数组下标匹配嵌入型记录的某个字段
如果你知道嵌入型记录的数组顺序,可以用以下查询db.testData.find( { 'memos.0.by': 'shipping' } )//匹配memos数组中第一元素的by字段为shipping的记录
无需数组下标匹配字段
不知道数组顺序的情况下,用以下查询db.testData.find( { 'memos.by': 'shipping' } )//匹配memos数组中某个元素包含by字段且值为shipping的记录
数组的多条件查询
和数组查询一样,使用$elemMatch就是要数组中的某个元素满足所有条件,不用的话就是多个元素组合起来满足所有条件单元素条件查询
使用$elemMatch来查询数组中某个元素满足所有的记录db.testData.find(
{
memos:
{
$elemMatch:
{
memo: 'on time',
by: 'shipping'
}
}
}
)//匹配memos中某个元素既有memo为'on time'又有by为 'shipping'的记录
组合满足条件查询
db.inventory.find({
'memos.memo': 'on time',
'memos.by': 'shipping'
}
)//匹配memos数组中某个元素包含memo字段且值为on time,同时另一个元素包含by字段且值为shipping的记录,或者某个元素同时满足两条记录

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

It is recommended to use the latest version of MongoDB (currently 5.0) as it provides the latest features and improvements. When selecting a version, you need to consider functional requirements, compatibility, stability, and community support. For example, the latest version has features such as transactions and aggregation pipeline optimization. Make sure the version is compatible with the application. For production environments, choose the long-term support version. The latest version has more active community support.

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

The data of the MongoDB database is stored in the specified data directory, which can be located in the local file system, network file system or cloud storage. The specific location is as follows: Local file system: The default path is Linux/macOS:/data/db, Windows: C:\data\db. Network file system: The path depends on the file system. Cloud Storage: The path is determined by the cloud storage provider.

The MongoDB database is known for its flexibility, scalability, and high performance. Its advantages include: a document data model that allows data to be stored in a flexible and unstructured way. Horizontal scalability to multiple servers via sharding. Query flexibility, supporting complex queries and aggregation operations. Data replication and fault tolerance ensure data redundancy and high availability. JSON support for easy integration with front-end applications. High performance for fast response even when processing large amounts of data. Open source, customizable and free to use.

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

MongoDB is a document-oriented, distributed database system used to store and manage large amounts of structured and unstructured data. Its core concepts include document storage and distribution, and its main features include dynamic schema, indexing, aggregation, map-reduce and replication. It is widely used in content management systems, e-commerce platforms, social media websites, IoT applications, and mobile application development.
