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



Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

Editor of the Machine Power Report: Yang Wen The wave of artificial intelligence represented by large models and AIGC has been quietly changing the way we live and work, but most people still don’t know how to use it. Therefore, we have launched the "AI in Use" column to introduce in detail how to use AI through intuitive, interesting and concise artificial intelligence use cases and stimulate everyone's thinking. We also welcome readers to submit innovative, hands-on use cases. Video link: https://mp.weixin.qq.com/s/2hX_i7li3RqdE4u016yGhQ Recently, the life vlog of a girl living alone became popular on Xiaohongshu. An illustration-style animation, coupled with a few healing words, can be easily picked up in just a few days.

When Sora failed to come out, OpenAI's opponents used their weapons to destroy the streets. If Sora is not open for use, it will really be stolen! Today, San Francisco startup LumaAI played a trump card and launched a new generation of AI video generation model DreamMachine. Free and available to everyone. According to reports, the model can generate high-quality, realistic videos based on simple text descriptions, with effects comparable to Sora. As soon as the news came out, a large number of users crowded into the official website to try it out. Although officials claim that the model can generate 120-frame video in just two minutes, many users have been waiting for hours on the official website due to a surge in visits. BarkleyDai, Luma’s head of product growth, had to comment on Discord

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

At 2:00 pm on July 31, Beijing time, the latest member of the XREAL series of AR glasses, XREAL Air2 Ultra, was officially launched in China. It is currently available on JD.com, Tmall, Douyin and other platforms, with an initial price of 3,999 yuan. This AR glasses is a flagship product mainly for the developer community. It aims to lower the threshold for developers to enter spatial computing, promote innovation in the field of spatial computing, and establish a more prosperous AR ecosystem. Empowering developers with six core capabilities As XREAL’s second 6DoF (Six Degrees of Freedom, six degrees of freedom) full-featured glasses, XREAL Air2 Ultra is currently the only one in the industry that uses dual environment sensing sensors (SLAM Camera)

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

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).
