https://github.com/vlang/mongo
Official MongoDB driver
https://github.com/vlang/mongo
Last synced: about 1 year ago
JSON representation
Official MongoDB driver
- Host: GitHub
- URL: https://github.com/vlang/mongo
- Owner: vlang
- License: mit
- Created: 2020-11-23T13:23:41.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-09-06T11:32:21.000Z (over 1 year ago)
- Last Synced: 2025-01-16T07:26:48.572Z (about 1 year ago)
- Language: V
- Size: 671 KB
- Stars: 47
- Watchers: 29
- Forks: 8
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Roadmap: ROADMAP.md
Awesome Lists containing this project
- awesome-v - mongodb - A MongoDB driver for V. (Libraries / Database clients)
README
## Mongo
_MongoDB driver for vlang_
### Getting start:
#### Installing dependeces
[libmongoc](http://mongoc.org/libmongoc/current/installing.html#install-libmongoc-with-a-package-manager)(MongoDB C Driver)
[libbson](http://mongoc.org/libmongoc/current/installing.html#install-libbson-with-a-package-manager)(BSON library)
```bash
# Debian and Ubuntu
sudo apt-get install libmongoc-dev
sudo apt-get install libbson-1.0-0
```
```bash
# Fedora
dnf install mongo-c-driver
dnf install libbson
```
```bash
# CentOS and RHEL 7
yum install mongo-c-driver
yum install libbson
```
```bash
# macOS
brew install mongo-c-driver
```
#### Starting MongoDB
```bash
mongo --host localhost --port 27017
```
#### Installing mongo package from `vpm`
```bash
v install mongo
```
**Examples**
```v
// connect to mongo
mongo_uri := mongo.uri_new('mongodb://127.0.0.1:27017')
client := mongo_uri.new_client()
// select database
database := client.get_database('db_name')
// select collection
collection := client.get_collection('db_name', 'collection_name')
// insert collection
collection.insert_one({
'str': 'string'
'number': 2
'float': 2.1
'boolean': true
})
// find collection
response := collection.find({
'str': 'string'
}).lean()
assert response[0].as_map()['str'] or { 0 }.str() == 'string'
```