You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2016-10-10-mongodb-console-commands.md
+42-20
Original file line number
Diff line number
Diff line change
@@ -9,23 +9,24 @@ categories: [mongodb]
9
9
tags: [codingmarks, mongodb]
10
10
---
11
11
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...
16
17
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
+
> .
19
19
20
20
{% include source-code-codingpedia-bookmarks.html %}
21
21
22
-
> As a prerequisite one should have a Mongo Server instance running.
23
-
24
22
<!--more-->
25
23
24
+
* TOC
25
+
{:toc}
26
+
26
27
## Start the mongo shell
27
28
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:
29
30
30
31
```bash
31
32
>cd<mongodb installation dir>
@@ -38,7 +39,7 @@ and then type `./bin/mongo` to start mongo
38
39
39
40
MongoDB does not enable access control by default. This might be fine for development, but for a production environment it is highly recommended
40
41
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.
42
43
43
44
## Quit the mongo shell
44
45
@@ -50,7 +51,7 @@ You can type the following command to exit the mongo shell:
50
51
51
52
## Display database and collections
52
53
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:
54
55
55
56
```bash
56
57
> show dbs
@@ -66,7 +67,7 @@ To switch to the _codingpedia-bookmarks_ database, use the `use` command:
66
67
switched to db codingpedia-bookmarks
67
68
```
68
69
69
-
Then print a list of all collections of the current database
70
+
Print the list of all collections of the current database:
70
71
71
72
```bash
72
73
> show collections
@@ -98,23 +99,43 @@ Response
98
99
```
99
100
### Find documents with filter
100
101
101
-
Filter by one attribute (here filter by location):
102
+
Filter by one attribute (here filter by **location**):
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 forall documents where the field `tags` value is an array with exactly three elements, "angular", "angular-cli" and "dev-tools",in the specified order:
107
115
```
108
-
> db.bookmarks.find(more attributes maybe with tag)
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,
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
+
```
112
133
113
134
### Sort results
114
135
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.
116
137
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):
118
139
docs
119
140
120
141
``` bash
@@ -136,7 +157,8 @@ Removes all bookmarks for user, identified by userId
0 commit comments