查询操作 基本查询 查询指定集合中的所有记录 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的记录,或者某个元素同时满足两条记录

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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