MongoDBのの正しい姿勢nodejs操作を使用します

MongoDBは、卵と豆腐の自然なフィット感のような保存文書データベース、およびクエリデータ基本的にJSON形式、MongoDBのとnodejsのタイプです。

nodejs操作を使用する前にMongoDBの階層を理解する必要があるのMongoDB:

  • システム(MongoDBは、いくつかのデータベースをインストールします)
    • 管理者
  • userdefind(ユーザ定義データベース)
    • データベース(データベース名)
      • コレクション(テーブルのセット)
        • コレクション(コレクション)
      • 機能
      • ユーザー

公式ドライバmongdb NPMツールを提供nodejsためのMongoDBは直接ロード使用することができます。モデルnodejs特性が、そうすべての関連操作をMongoDBの使用またはコールバックの約束の方法を介して。

内蔵環境

  • インストールnodejs
  • NPM熱
  • package.json依存関係を追加

データベースのすべてを取得します

API:admin.listDatabases

//回调版
const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
    client.db("").admin().listDatabases(function (err, resp) {
        resp.databases.forEach(function (value, index, arr) {
            console.log(value.name);
        })
    })
});

//promise版

const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
    client.db("").admin().listDatabases().then(function (data) {
        data.databases.forEach(function (value, index, arr) {
            console.log(value.name);
        })
    }, function (err) {
        console.log(err.message);
    })
});

データベースを作成します。

MongoDBのデータベースが存在しない場合、それは自動的にデータベースを作成します、したがって、あなたが意図的にデータベースを操作するために作成することはできません

コレクションを作成します。

const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
    client.db("example").createCollection("table1").then(function (result) {
    }, function (err) {
        console.log(err.message);
    })
});

すべてのコレクションを取得します。

const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
    client.db("example").listCollections().toArray().then(function (tables) {
        tables.forEach(function (value, index, ts) {
            console.log(value.name);
        })
    }, function (err) {
        console.log(err.message);
    })
});

データ収集を挿入

const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
    client.db("example").collection("table1").insert({ name: "张三丰" }).then(function (result) {
    }, function (err) {
        console.log(err.message);
    })
});

すべてのデータのコレクションを取得します。

const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
    client.db("example").collection("table1").find({}).toArray().then(function (result) {
        result.forEach(function (value, index, arr) {
            console.log(value.name);
        });
    }, function (err) {
        console.log(err.message);
    })
});

データを変更します

const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
    client.db("example").collection("table1").update({ name: "李斯" }, { age: 100,name: "李斯" }).then(function (result) {
        console.log(result.result);
    }, function (err) {
        console.log(err.message);
    })
});

条件による問合せ

const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
    client.db("example").collection("table1").find({ age: 100 }).toArray().then(function (result) {
        result.forEach(function (value, index, arr) {
            console.log(value);
        })
    }, function (err) {
        console.log(err.message);
    })
});

データを削除します。

const mongoclient = require("mongodb").MongoClient;
const url = "";
mongoclient.connect(url, function (err, client) {
    client.db("example").collection("table1").deleteMany({ age: 100 }).then(function (result) {
        console.log(result.result);
    }, function (err) {
        console.log(err.message);
    })
});

シンプルなMongoDBのデータ複製プロジェクトhttps://github.com/golangaccount/mongodb_simple_clone

おすすめ

転載: www.cnblogs.com/zp900704/p/11688041.html