« Mongodb » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 160 : Ligne 160 :
db.myCollection.find( { name: /^item/ } )
db.myCollection.find( { name: /^item/ } )
// SELECT ... WHERE name LIKE "item%"
// SELECT ... WHERE name LIKE "item%"
db.myCollection.find( { status: { $in: [ "active", "deleted" ] } } )
// SELECT ... WHERE status in ("active", "deleted")


db.myCollection.find( { status: "active", quantity: { $lt: 10 } } )
db.myCollection.find( { status: "active", quantity: { $lt: 10 } } )
Ligne 170 : Ligne 167 :
// SELECT ... WHERE status = "active" OR quantity < 10
// SELECT ... WHERE status = "active" OR quantity < 10


 
db.myCollection.find( { status: { $in: [ "active", "deleted" ] } } )
// SELECT ... WHERE status in ("active", "deleted")
</kode>
</kode>
* [https://www.mongodb.com/docs/manual/reference/operator/query/regex regex]
* [https://www.mongodb.com/docs/manual/reference/operator/query/regex regex]

Version du 21 février 2024 à 10:35

Description

MongoDB stores data records as BSON documents gathered in collections (= SQL table). BSON is a binary representation of JSON documents.
MongoDB documents are composed of field-and-value pairs. The value of a field can be any of the BSON data types.

Json.svg
var mydoc = {
               // field _id is a primary key
               _id: ObjectId("5099803df3f4948bd2f98391"),
               // name holds an embedded document that contains the fields first and last
               name: { first: "Alan", last: "Turing" },
               birth: new Date('Jun 23, 1912'),
               death: new Date('Jun 07, 1954'),
               contribs: [ "Turing machine", "Turing test", "Turingery" ],
               views : NumberLong(1250000)
            }

Connect and authenticate

Bash.svg
# connect
mongosh mongodb://localhost:27017
# alias
mongosh

# connect and authenticate, use an environment variable to hide the password
mongosh "mongodb://user:${DBPASSWORD}@<host>:<port>/admin?authSource=admin"

Authenticate

Mongodb.svg
db.auth("username", "pwd")

db.auth("username", passwordPrompt())

Database

Mongodb.svg
// list the databases
show dbs

// switch to [dbname]
use [dbname]

// create a new database with a new collection (and insert data)
use [newdbname]
db.[newCollectionName].insertOne( { x: 1 } )

// drop the current database
db.dropDatabase()

Collection

It gathers documents and is an equivalent of an SQL table.
A collection does not require its documents to have the same schema (the same set of fields and the same data types)

Mongodb.svg
// list collections
db.getCollectionInfos()
db.runCommand('listCollections')

db.createCollection("[collectionName]",
{
  [option]: [value]
})

View

It is a read-only queryable object whose contents are defined by an aggregation pipeline on other collections or views.
It is computed when you read the view, and it is not stored to disk.
Create and Query a View

Mongodb.svg
// 2 syntaxes to create a view
db.createCollection(
  "<viewName>",
  {
    "viewOn" : "<source>",
    "pipeline" : [<pipeline>],
    "collation" : { <collation> }
  }
)

db.createView(
  "<viewName>",
  "<source>",
  [<pipeline>],
  {
    "collation" : { <collation> }
  }
)

// drop a view
db.<viewName>.drop()

Time Series Collections

MongoDB treats time series collections as writable non-materialized views backed by an internal collection. Create and Query a Time Series Collection

Mongodb.svg
db.createCollection(
"weather",
{
  timeseries: {
  timeField: "timestamp",
  metaField: "metadata",
  granularity: "seconds"
}})

Densify and fill

  • densify allows to create missing spots in the TS
    • bounds: "full" spans from the full range of the original TS
    • bounds: [ <from>, <to> ] spans from the defined bounds
  • fill allows to set values in those new spots.
    • locf to get the same value as the previous spot
    • linear to get the linear interpolation between the previous and the next point
Mongodb.svg
[{
  $densify: {
    field: "event",
    range: {
      step: 15,
      unit: "minute",
      bounds: "full"
    }
  }
}, {
  $fill: {
    sortBy: {
      event: 1
    },
    output: {
      capacity: {
        method: "locf"
      }
    }
  }
}]

Query

find

Mongodb.svg
db.myCollection.find( { } )
// SELECT *

db.myCollection.find( { item: 1, status: 1 } )
// SELECT _id, item, status
// _id is returned by default

db.myCollection.find( { item: 1, status: 1, _id: 0 } )
// SELECT item, status

db.myCollection.find( { status: 0 } )
// returns all fields but status

filter

Mongodb.svg
db.myCollection.find( { name: "item1" } )
// SELECT ... WHERE name = "item1"

db.myCollection.find( { name: /^item/ } )
// SELECT ... WHERE name LIKE "item%"

db.myCollection.find( { status: "active", quantity: { $lt: 10 } } )
// SELECT ... WHERE status = "active" AND quantity < 10

db.myCollection.find( { $or: [ { status: "active" }, { qty: { $lt: 10 } } ] } )
// SELECT ... WHERE status = "active" OR quantity < 10

db.myCollection.find( { status: { $in: [ "active", "deleted" ] } } )
// SELECT ... WHERE status in ("active", "deleted")

Backup and restore

Bash.svg
# restore the backup folder dump to the local mongodb instance
mongorestore dump/

don't know what to do with file while restore

This may happen because the mongorestore command point to a backup sub-folder not to the backup folder itself.

Authentication

Mongodb.svg
// list the users of the current database
db.getUsers()
db.runCommand('usersInfo')

// get info of a specific user
db.getUser("tom", { showCredentials: true, showPrivileges: true, showAuthenticationRestrictions: true })

// create admin
use admin
db.createUser(
  {
    user: "admin",
    pwd: "mypwd",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

// create a user for the demo database
use demo
db.createUser(
  {
    user: "tom",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "readWrite", db: "demo" },
             { role: "read", db: "finances" } ]
  }
)

// change password
use admin
db.changeUserPassword("tom", "secretpassword")
db.changeUserPassword("tom", passwordPrompt())

// delete a user
db.dropUser("tom")
/etc/mongod.conf
security:
authorization: "enabled"

Bash

Bash.svg
mongo <<EOF
    use admin
    db.auth("admin", "${MONGODBADMINPWD}")
    use mydb
    db.dropDatabase()
EOF

Compass

Installation

Windows

Ps.svg
choco install mongodb mongodb-shell
Key Value
Service name MongoDB
Data directory C:\Program Files\MongoDB\Server\6.0\data\
Log directory C:\Program Files\MongoDB\Server\6.0\log\

Archlinux

Bash.svg
yay mongodb-bin
# install dependencies mongodb-bin-debug, mongosh-bin, mongosh-bin-debug

# GUI
yay mongodb-compass

# tools
yay mongodb-tools

# service
sc-status mongodb.service