mongodb入门-9查询4
mongodb入门-9查询4 mongodb入门-8查询3 http://www.2cto.com/database/201305/212158.html $elemMatch 匹配内嵌文章中的数据,我们知道在mongodb中可以使用内嵌文档,但是这时候根据内嵌文档的内容查询使用原先介绍的方法,可能就不是那么方便了,好在mongodb已
mongodb入门-9查询4
mongodb入门-8查询3
http://www.2cto.com/database/201305/212158.html
$elemMatch
匹配内嵌文章中的数据,我们知道在mongodb中可以使用内嵌文档,但是这时候根据内嵌文档的内容查询使用原先介绍的方法,可能就不是那么方便了,好在mongodb已经考虑了,为我们提供了$elemMatch方法.例子:
[html]
> db.user.find()
{ "_id" : ObjectId("5198c286c686eb50e2c843b2"), "name" : "user0", "age" : 0 }
{ "_id" : ObjectId("5198c286c686eb50e2c843b3"), "name" : "user1", "age" : 1 }
{ "_id" : ObjectId("5198c286c686eb50e2c843b4"), "name" : "user2", "age" : 2 }
{ "_id" : ObjectId("5198c286c686eb50e2c843b5"), "name" : "user3", "age" : 1 }
{ "_id" : ObjectId("5198c286c686eb50e2c843b6"), "name" : "user4", "age" : 1 }
{ "_id" : ObjectId("5198c286c686eb50e2c843b7"), "name" : "user5", "age" : 2 }
{ "_id" : ObjectId("51997c702b76790566165e48"), "name" : "xiangyue", "age" : 23, "title" : [ { "content" : "aaa" }, {
"content" : "bbb" }, { "discuss" : "cc" } ] }
{ "_id" : ObjectId("51997c9f2b76790566165e49"), "name" : "xiangyue", "age" : 23, "title" : [ { "content" : "dd" }, {
"content" : "ee" }, { "discuss" : "ff" } ] }
> db.user.find({title:{$elemMatch : {content : "aaa"}}}) -->查找title中的content字段为aaa的文档
{ "_id" : ObjectId("51997c702b76790566165e48"), "name" : "xiangyue", "age" : 23, "title" : [ { "content" : "aaa" }, {
"content" : "bbb" }, { "discuss" : "cc" } ] }
游标
mongodb中提供了像关系型数据库中的游标操作.首先我们获取一个游标,获取游标很简单,只需要将我们以前学过的查询方法赋值给一个变量就可以了:
[html]
var cursor = db.test.find()
我们如果在命令行直接输入cursor,将会执行这个查询打印结果,这个时候我们再去执行cursor的时候将神马都不会返回了,因为这个游标已经使用完了,当然我们不想这样使用游标,我们可以像下面那样使用游标:
[html]
> while(cursor.hasNext()){ var current = cursor.next(); print("age=" + current.age); }
age=1
age=2
age=3
age=4
age=5
当然我们可以使用游标的forEach方法,如下:
[html]
> var cursor = db.test.find()
> cursor.forEach(function(obj){
... print("age=" + obj.age);
... });
age=1
age=2
age=3
age=4
age=5
同样游标可以结合sort/skip/limit等方法一同使用,换句话说以前学的基本所有的查询都可以跟游标结合使用.
count 计算查询结果的数目
这个比较简单,在以前的查询的命令后面再加上.count()就可以了.
[html]
> db.test.find()
{ "_id" : 1, "name" : "user_1", "age" : 1 }
{ "_id" : 2, "name" : "user_2", "age" : 2 }
{ "_id" : 3, "name" : "user_3", "age" : 3 }
{ "_id" : 4, "name" : "user_4", "age" : 4 }
{ "_id" : 5, "name" : "user_5", "age" : 5 }
> db.test.find().count()
5
null查询
例如下面文档我们只想查出y值为null的文档,
[html]
> db.test2.find()
{ "_id" : ObjectId("519984432b76790566165e4c"), "x" : null }
{ "_id" : ObjectId("5199844c2b76790566165e4d"), "y" : null, "x" : "test" }
按照我们原来学过的使用下的方式查询结果不是我们想要的:
[html]
> db.test2.find({y:null}) -->我们发现mongodb对于y不存在也会给查出来
{ "_id" : ObjectId("519984432b76790566165e4c"), "x" : null }
{ "_id" : ObjectId("5199844c2b76790566165e4d"), "y" : null, "x" : "test" }
这里提供一下两种方法查询
[html]
> db.test2.find({y:{$type:10}}) -->明确指出其类型
{ "_id" : ObjectId("5199844c2b76790566165e4d"), "y" : null, "x" : "test" }
> db.test2.find({y:null,y:{$exists:1}}) -->查找null 但是同时y值必须存在
{ "_id" : ObjectId("5199844c2b76790566165e4d"), "y" : null, "x" : "test" }

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

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

The matrix is difficult to understand, but it may be different if you look at it from another perspective. When learning mathematics, we are often frustrated by the difficulty and abstractness of the knowledge we learn; but sometimes, just by changing the perspective, we can find a simple and intuitive solution to the problem. For example, when we were learning the formula for the sum of squares (a+b)² when we were children, we may not understand why it is equal to a²+2ab+b². We only knew that it was written like this in the book and the teacher asked us to remember it like this; until one day we saw I saw this animated picture: It suddenly dawned on me that we can understand it from a geometric perspective! Now, this sense of enlightenment occurs again: a non-negative matrix can be equivalently converted into the corresponding directed graph! As shown in the figure below, the 3×3 matrix on the left can actually be

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

According to news on September 4, market research agency Counterpoint Research recently released market data for entry-level phones in the second quarter of 2024. It shows that global economical smartphone sales exceeded 100 million units in the quarter, a year-on-year increase of 10%, accounting for 37% of global smartphone sales. The top 10 best-selling budget smartphones reached a quarter, up from 21% in the same period last year. 1. Xiaomi, Samsung and OPPO each occupy three spots in the top ten rankings, and the remaining one spot belongs to Transsion. Redmi13C was the biggest winner, with the 4G version winning the championship and the 5G version ranking fourth, becoming the far leading single product. As the only 5G model on the list, Redmi13C5G fulfills Xiaomi’s classic slogan “
