Table of Contents
枚举遍历指针
概述
手动枚举指针
枚举下标
Home Database Mysql Tutorial MongoDB操作手册CRUD查询指针

MongoDB操作手册CRUD查询指针

Jun 07, 2016 pm 04:06 PM
crud mongodb pointer enumerate Inquire Traverse

枚举遍历指针 概述 前面已经讲过,db.collection.find()如果没有指定给一个var声明的变量,将自动枚举前20条记录。 手动枚举指针 在mongo控制台中,将查询赋给一个var声明的变量,让其不自动枚举。 var cur = db.testData.find(); 然后每次调用这个指针,将

枚举遍历指针

概述

前面已经讲过,db.collection.find()如果没有指定给一个var声明的变量,将自动枚举前20条记录。

手动枚举指针

在mongo控制台中,将查询赋给一个var声明的变量,让其不自动枚举。
var cur = db.testData.find();
然后每次调用这个指针,将自动遍历20条
cur;
也可以使用指针的next()方法来获取下一条记录
var cur = db.testData.find();
while(cur.hasNext())
{
print(tojson(cur.next()));//此处打印操作可以用printjson来替换:printjson(cur.next());
}
可以用指针的forEach()方法来遍历指针数据:
var cur = db.testData.find();
cur.forEach(printjson);

枚举下标

在mongo控制台中,可以使用toArray()方法来访问指针结果。
var cur=db.testData.find();
var arr = cur.toArray();
var item = arr[2];
toArray()方法将加载所有查询结果到内存,这个方法将遍历完整个指针。
另外,一些驱动提供了直接使用数组下标的方式,这个方式是调用了toArray()的缩写。
var cur=db.testData.find();
var item = cur[3];
以上两句等同于cur.toArray()[3];

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In-depth analysis of pointers and references in C++ to optimize memory usage In-depth analysis of pointers and references in C++ to optimize memory usage Jun 02, 2024 pm 07:50 PM

By using pointers and references, memory usage in C++ can be optimized: Pointers: store addresses of other variables and can point to different variables, saving memory, but may generate wild pointers. Reference: Aliased to another variable, always points to the same variable, does not generate wild pointers, and is suitable for function parameters. Optimizing memory usage can improve code efficiency and performance by avoiding unnecessary copies, reducing memory allocations, and saving space.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

What is the use of net4.0 What is the use of net4.0 May 10, 2024 am 01:09 AM

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

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

How does a C++ pointer reference an object? How does a C++ pointer reference an object? Jun 03, 2024 pm 01:00 PM

In C++, a pointer can refer to an object. The steps include declaring a pointer variable, obtaining the object address, and assigning it to the pointer. This allows programmers to access and modify an object's properties and methods through pointers.

How do pointers implement dynamic memory allocation? How do pointers implement dynamic memory allocation? Jun 05, 2024 pm 01:13 PM

Pointers and Dynamic Memory Allocation: Pointers are a feature in programming languages ​​used to store the address of another block of memory. By using pointers, the required memory can be allocated as needed at runtime. Use an allocator function such as malloc() or new to store the memory address in a pointer variable. Practical case: Use pointers to dynamically allocate an array to store student grades read from a text file.

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

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

How are pointers and arrays related in C++? How are pointers and arrays related in C++? Jun 01, 2024 am 09:52 AM

Pointers and arrays are closely related in C++: pointers store variable addresses, while arrays are essentially collections of contiguous memory cells. The array name is a constant pointer pointing to the first element of the array. Pointer arithmetic can be used to iterate over array elements, similar to using array indexing.

See all articles