# Mongo

**Export database**:

```sh
mongodump --host <database-host> -d <database-name> --port <database-port> --out directory
```

**Import database**:

```sh
mongorestore --host <database-host> -d <database-name> --port <database-port> foldername
```

uri is also an option:

```sh
mongorestore --uri="uri" --dir="dir"
```

**Search by regex**

```js
db.collectionname.find({"name": {'$regex':'^File'}})
# All the documents that start with File
```

**Update field for a existing document**

```js
# Example
db.agents.find().forEach((doc) => {  doc.tags[0] = doc.tags[0].replace(">", "-"); db.agents.save(doc); });
```

## Python utils

**count records**

```python
collection.count_documents(query_string)
```

**Make sure update was done**

```python
try:
    result = self.settings_store.admin_users.update_one(user_query, language_insert)
    return result.matched_count > 0
except errors.PyMongoError as e:
    return False
```

## Useful tools

* [restheart](https://restheart.org/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wilmerags.gitbook.io/digital-garden/code/databases/mongo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
