Skip to content

Commit 6e5fdef

Browse files
author
Adrian Matei
committed
add some corrections to mongo post
1 parent 4063935 commit 6e5fdef

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

_posts/2016-10-10-mongodb-console-commands.md

+42-20
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@ categories: [mongodb]
99
tags: [codingmarks, mongodb]
1010
---
1111

12-
Public and private [codingmarks](https://www.codingmarks.org/) are persisted in a MongoDB Server](https://docs.mongodb.com/manual/) to persist private and
13-
public bookmarks. Very often I find myself in the situation, where I need to modify or look for something in the mongo database.
14-
What do I do then? Well I google it, and most likely I am pointed to the right entry in the [Mongo manual](https://docs.mongodb.com/manual/).
15-
With this post I try to consolidate the commands I usually use so that I have one place to come to later...
12+
The [codingmarks](https://www.codingmarks.org/) are persisted in a [MongoDB Server](https://docs.mongodb.com/manual/).
13+
Very often I find myself in the situation, where I need to modify or look for something in the mongo database.
14+
Experience has taught me that interacting with a system via shell commands helps me understand it better,
15+
and sort of brings me closer to it. Ok, so how to find the right mongo shell command? Well, I google for it of course, and most likely I am pointed to the right entry in the [Mongo manual](https://docs.mongodb.com/manual/). In this post I try to consolidate the commands I usually use,
16+
so that I have only one [codingmark](https://www.codingmarks.org/search?q=codingpedia mongo shell commands) to look for...
1617

17-
> Experience has taught me that interacting with a system via shell commands helps you at understand it better,
18-
and sort of brings you closer to it.
18+
> .
1919
2020
{% include source-code-codingpedia-bookmarks.html %}
2121

22-
> As a prerequisite one should have a Mongo Server instance running.
23-
2422
<!--more-->
2523

24+
* TOC
25+
{:toc}
26+
2627
## Start the mongo shell
2728

28-
To start the mongo shell and connect to the MongoDB instance running on localhost with default port, change to the MongoDB installation directory:
29+
To start the mongo shell and connect to the MongoDB instance running on **localhost** with **default port**(which is 27017), change to the MongoDB installation directory:
2930

3031
``` bash
3132
> cd <mongodb installation dir>
@@ -38,7 +39,7 @@ and then type `./bin/mongo` to start mongo
3839
3940
MongoDB does not enable access control by default. This might be fine for development, but for a production environment it is highly recommended
4041
to employ [authorization](https://docs.mongodb.com/manual/core/authorization/). Please see the **Create database and users** section of
41-
the [MongoDB Setup For Production](https://github.com/Codingpedia/codingmarks-api/wiki/MongoDB-Setup-for-Production) wiki page for commands related to this.
42+
the [MongoDB Setup For Production](https://github.com/Codingpedia/codingmarks-api/wiki/MongoDB-Setup-for-Production) wiki page for commands related to that.
4243

4344
## Quit the mongo shell
4445

@@ -50,7 +51,7 @@ You can type the following command to exit the mongo shell:
5051

5152
## Display database and collections
5253

53-
Once in, you can print a list of all available database on the server via `show dbs`:
54+
You can print a list of all available database on the server via `show dbs` command:
5455

5556
``` bash
5657
> show dbs
@@ -66,7 +67,7 @@ To switch to the _codingpedia-bookmarks_ database, use the `use` command:
6667
switched to db codingpedia-bookmarks
6768
```
6869

69-
Then print a list of all collections of the current database
70+
Print the list of all collections of the current database:
7071

7172
``` bash
7273
> show collections
@@ -98,23 +99,43 @@ Response
9899
```
99100
### Find documents with filter
100101
101-
Filter by one attribute (here filter by location):
102+
Filter by one attribute (here filter by **location**):
102103
```
103104
> db.bookmarks.find({location: "http://www-cs-students.stanford.edu/~blynn/gitmagic/intl/zh_cn/"});
104105
{ "_id" : ObjectId("5948ab65ce8e01b7e330b330"), "name" : "Git magic", "location" : "http://www-cs-students.stanford.edu/~blynn/gitmagic/intl/zh_cn/", "tags" : [ "free-programming-books-zh", "版本控制" ], "description" : "", "descriptionHtml" : "<p></p>", "createdAt" : ISODate("2017-06-20T04:58:13.192Z"), "shared" : true, "userId" : "2d6f6cb5-44f3-441d-a0c5-ec9afea98d39", "language" : "zh" }
105106
```
106107
108+
Check out the [find documention](https://docs.mongodb.com/manual/reference/method/db.collection.find/) for more details.
109+
110+
### [Query an array](https://docs.mongodb.com/manual/tutorial/query-arrays/)
111+
112+
#### Match an array
113+
114+
Query for all documents where the field `tags` value is an array with exactly three elements, "angular", "angular-cli" and "dev-tools", in the specified order:
107115
```
108-
> db.bookmarks.find(more attributes maybe with tag)
116+
> db.bookmarks.find( { tags: ["angular", "angular-cli", "dev-tools"] } )
109117
```
110118
111-
Check out the [find documention](https://docs.mongodb.com/manual/reference/method/db.collection.find/) for more details.
119+
If, instead, we wish to find an array that contains all the elements "angular", "angular-cli" and "dev-tools", without regard to order or other elements in the array,
120+
we use the `$all` operator:
121+
```
122+
> db.bookmarks.find( { tags: { $all: ["angular", "angular-cli", "dev-tools"] } } )
123+
```
124+
125+
#### Query an Array for an Element
126+
127+
To query if the array field contains at least one element with the specified value, use the filter `{ <field>: <value> }` where `<value>` is the element value.
128+
129+
The following example queries for all documents where `tags` is an array that contains the string "angular" as one of its elements:
130+
```
131+
> db.bookmarks.find({tags:"angular-cli"})
132+
```
112133
113134
### Sort results
114135
115-
You can apply `sort()`` to the cursor before retrieving any documents from the database.
136+
You can apply `sort()` to the cursor before retrieving any documents from the database.
116137
117-
Example Sort documents by `updatedAt` Date (`1` for ascending and `-1` for descending):
138+
For example let's sort documents by `updatedAt` Date (`1` for ascending and `-1` for descending):
118139
docs
119140
120141
``` bash
@@ -136,7 +157,8 @@ Removes all bookmarks for user, identified by userId
136157
> db.bookmarks.remove({userId:"2d6f6cb5-44f3-441d-a0c5-ec9afea98d39"});
137158
WriteResult({ "nRemoved" : 4 })
138159
```
139-
Remove with attributes (all but shared true):
160+
161+
Remove with attributes (all but where `shared` is not set to true):
140162
```bash
141163
> db.bookmarks.remove({shared:{$ne:true}})
142164
WriteResult({ "nRemoved" : 9 })
@@ -146,14 +168,14 @@ For more details see the [db.collection.remove() documentation](https://docs.mon
146168
147169
## Update documents
148170
149-
## Update field value
171+
### Update field value
150172
Update `githubURL` for document with the given `location` (we know that is unique):
151173
152174
```
153175
> db.bookmarks.update({ location : "http://www.codingpedia.org/" }, { githubURL : "https://github.com/Codingpedia/codingpedia.github.io"} );
154176
```
155177
156-
## Add new field
178+
### Add new field
157179
158180
Add new `language` field and set it to `en` for all documents:
159181
```

0 commit comments

Comments
 (0)