Find or Query Data with the mongo Shell

https://docs.mongodb.com/getting-started/shell/query/

Overview

You can use the find() method to issue a query to retrieve data from a collection in MongoDB.

All queries in MongoDB have the scope of a single collection.

Queries can return all documents in a collection or only the documents that match a specified filter or criteria.

You can specify the filter or criteria in a document and pass as a parameter to the find() method.

The find() method returns query results in a cursor, which is an iterable object that yields documents.

Prerequisites

The examples in this section use the restaurants collection in the test database.

For instructions on populating the collection with the sample dataset, see Import Example Dataset.

In the mongo shell connected to a running mongod instance, switch to the test database.

use test

Query for All Documents in a Collection

To return all documents in a collection, call the find() method without a criteria document.

For example, the following operation queries for all documents in the restaurants collection.

db.restaurants.find()

The result set contains all documents in the restaurants collection.

如果collection name是数字的,那么

db["713000007000"].find()

时间: 2024-12-14 13:21:25

Find or Query Data with the mongo Shell的相关文章

MongoDB - The mongo Shell, Data Types in the mongo Shell

MongoDB BSON provides support for additional data types than JSON. Drivers provide native support for these data types in host languages and the mongo shell also provides several helper classes to support the use of these data types in the mongo Java

MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell

The db.collection.find() method returns a cursor. To access the documents, you need to iterate the cursor. However, in the mongo shell, if the returned cursor is not assigned to a variable using the varkeyword, then the cursor is automatically iterat

count failed: not master{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" } at src/mongo/shell/query.js:191在SECONDARY节点无法show dbs

count failed: not master{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" } at src/mongo/shell/query.js:191 在SECONDARY节点无法show dbs 主从启动之后,连接slave可以成功连上,但是在slave中执行 show dbs 的时候就报错了: QUERY Er

MongoDB - Introduction of the mongo Shell

Introduction The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mongo shell to query and update data as well as perform administrative operations. The mongo shell is a component of the MongoDB distributions. Once you h

《转》couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145

couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145,有须要的朋友能够參考下. 应为昨天安装的时候没及时截图,语言表达有点差,谅解 昨天在安装mongodb的时候无故出现 couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145 这种一个错误.后来google了下,基本都是说的没有正常关闭.产品了个mongod

MongoDB - The mongo Shell, Write Scripts for the mongo Shell

You can write scripts for the mongo shell in JavaScript that manipulate data in MongoDB or perform administrative operation. For more information about the mongo shell, see the Running .js files via a mongo shell Instance on the Server section for mo

MongoDB - The mongo Shell, Configure the mongo Shell

Customize the Prompt You may modify the content of the prompt by setting the variable prompt in the mongo shell. The promptvariable can hold strings as well as JavaScript code. If prompt holds a function that returns a string, mongo can display dynam

MongoDB - The mongo Shell, mongo Shell Quick Reference

mongo Shell Command History You can retrieve previous commands issued in the mongo shell with the up and down arrow keys. Command history is stored in ~/.dbshell file. See .dbshell for more information. Command Line Options The mongo shell can be sta

《转》couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145

couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145,有需要的朋友可以参考下. 应为昨天安装的时候没及时截图,语言表达有点差,谅解 昨天在安装mongodb的时候无故出现 couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145 这样的一个错误,后来google了下,基本都是说的没有正常关闭,产品了个mongo