Mongodb operation

Time:2021-3-21

MongoDB

  1. loadmongodb

    const MongoClient = require('mongodb').MongoClient;
     const resuleSet = require('./resultSet');
     
     //Database address
     const url = 'mongodb://localhost:27017';
     //Database name
     const dbName = 'user';
     
     let db = {};
  2. Connect to database

     MongoClient.connect(url, function(err, client) {
       db = client.db(dbName);
     });
     
  3. Database operation

    module.exports = {
       //Collection is the collection, filter is the filter, and data is the data
       //Get data
       async select(collection, filter = {}) {
         // db.collection.find () this method obtains multiple data
         const result = await db.collection(collection).find(filter).toArray();
         return resuleSet(result.length > 0, result);
       },
     
       //Add data
       async insert(collection, data) {
         // db.collection.insert () this method inserts one or more pieces of data
         const result = await db.collection(collection).insert(data);
         return resuleSet(result.length > 0, result);
       },
     
       //Update data
       async update(collection, filter, data) {
         // db.collection.update () this method updates multiple data
         const result = await db.collection(collection).update(filter, data);
         return resuleSet(result.length > 0, result);
       },
     
       //Delete data
       async delete(collection, filter) {
         // db.collection.deleteOne () this method removes a single document from the collection
         const result = await db.collection(collection).deleteOne(filter);
         return resuleSet(result.length > 0, result);
       },
     };

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]