mongoose

在nodejs中操作数据库(MongoDB和MySQL为例)

强颜欢笑 提交于 2020-12-27 09:03:35
一、使用nodejs操作MongoDB数据库 ①使用官方的mongodb包来操作 ②使用第三方的mongoose包来操作(比较常用) // 首先必须使MongoDB数据库保持开启状态 // npm下载mongoose包并引入 var mongoose=require('mongoose' ); // 连接MongoDB数据库 mongoose.connect('mongodb://localhost/test',{useMongoClient: true }); mongoose.Promise = global.Promise; // 创建一个模型,即设计数据库 var Cat=mongoose.model('Cat' ,{name:String}); // 实例化一个Cat var kitty= new Cat({name:'hello world' }); // 持久化保存kitty实例 kitty.save( function (err,ret){ if (err){ console.log(err) } else { console.log(ret) } }) ③使用mongoose操作MongoDB数据库例子: 基本工作:开启MongoDB服务,连接数据库,npm安装包并引包、连接数据库、设计文档结构、将文档发布为模型 // 首先必须使MongoDB数据库保持开启状态

Reg: Data is not providing correct number in mongo aggregate function

﹥>﹥吖頭↗ 提交于 2020-12-23 18:32:10
问题 Using below query to get the output: db.action.aggregate([{ $match: { "$and": [{ "timeStamp": { "$gte": ISODate("2020-12-13T00:00:00.000-0400"), "$lt": ISODate("2020-12-15T23:59:59.000-0400") } }, { "functionStatus": "FMO_UVERSE_DTV" }] } }, { $group: { _id: { Dates: { $dateToString: { format: "%Y-%m-%d", date: "$timeStamp" } }, FS: "$functionStatus" }, JOBCOUNT: { $addToSet: "$jobId" } } }, { $project: { "_id": NumberInt(0), "Dates": "$_id.Dates", "FS": "$_id.FS", "Total_JOB_FMO_UVERSE_DTV":

Reg: Data is not providing correct number in mongo aggregate function

半世苍凉 提交于 2020-12-23 18:31:10
问题 Using below query to get the output: db.action.aggregate([{ $match: { "$and": [{ "timeStamp": { "$gte": ISODate("2020-12-13T00:00:00.000-0400"), "$lt": ISODate("2020-12-15T23:59:59.000-0400") } }, { "functionStatus": "FMO_UVERSE_DTV" }] } }, { $group: { _id: { Dates: { $dateToString: { format: "%Y-%m-%d", date: "$timeStamp" } }, FS: "$functionStatus" }, JOBCOUNT: { $addToSet: "$jobId" } } }, { $project: { "_id": NumberInt(0), "Dates": "$_id.Dates", "FS": "$_id.FS", "Total_JOB_FMO_UVERSE_DTV":

Reg: Data is not providing correct number in mongo aggregate function

别说谁变了你拦得住时间么 提交于 2020-12-23 18:31:05
问题 Using below query to get the output: db.action.aggregate([{ $match: { "$and": [{ "timeStamp": { "$gte": ISODate("2020-12-13T00:00:00.000-0400"), "$lt": ISODate("2020-12-15T23:59:59.000-0400") } }, { "functionStatus": "FMO_UVERSE_DTV" }] } }, { $group: { _id: { Dates: { $dateToString: { format: "%Y-%m-%d", date: "$timeStamp" } }, FS: "$functionStatus" }, JOBCOUNT: { $addToSet: "$jobId" } } }, { $project: { "_id": NumberInt(0), "Dates": "$_id.Dates", "FS": "$_id.FS", "Total_JOB_FMO_UVERSE_DTV":

详解node + mongoDb(mongoDb安装、运行,在node中连接、增删改查)

懵懂的女人 提交于 2020-12-18 02:42:22
一、序言 好久没写博客了,这次主要聊聊 node 和 mongoDb 。 先说明一下技术栈 node + express + mongoose + mongoDb。这篇博客,主要讲述 mongoDb 的下载 、安装 、 配置 、 运行 以及如何在 node 项目中引入 mongoose 并 链接 mongoDb 操作数据库,最后再附带几个简单 创建 数据库、集合、域的实例。 二、目录   1、 下载、安装 mongoDb   2、 配置、运行 mongoDb   3、 node项目中 链接 mongoDb   4、 node项目中对 mongoDb 实现 数据库 集合 域的创建、编辑等操作 三、下载、安装 mongoDb 官方下载链接: https://www.mongodb.com/download-center#community 下载 Windows 64-bit x64 msi 格式的,完事直接傻瓜式安装。或者你也可以选择 customs 自定义安装,去选择一些路径啥的 四、配置、运行 mongoDb 在任意没有中文的目录下新建文件夹,如c:\data,在文件夹下存放MongoDB数据库文件与日志文件,如: ps: 当然你也可以不配置   c:\dbData\db用于存放mongodb的数据文件   c:\ dbData \log用于存放mongodb的日志文件

Requirements for using MongoDB transactions

谁都会走 提交于 2020-12-15 07:32:36
问题 When creating a new ticket I am trying to use mongo transactions so that I could rollback the transaction if at least one of the "ticket saving process" or "Publishing the ticket created event" fails. Inside the try catch block I have manually thrown the error (throw new Error('ssomlk caused manual error');) so I could try to test whether the transaction works. const session = await mongoose.startSession(); session.startTransaction(); try { await ticket.save({ session }); throw new Error(

Requirements for using MongoDB transactions

◇◆丶佛笑我妖孽 提交于 2020-12-15 07:32:13
问题 When creating a new ticket I am trying to use mongo transactions so that I could rollback the transaction if at least one of the "ticket saving process" or "Publishing the ticket created event" fails. Inside the try catch block I have manually thrown the error (throw new Error('ssomlk caused manual error');) so I could try to test whether the transaction works. const session = await mongoose.startSession(); session.startTransaction(); try { await ticket.save({ session }); throw new Error(

How to solve Mongoose v5.11.0 model.find() error: Operation `products.find()` buffering timed out after 10000ms"

狂风中的少年 提交于 2020-12-15 06:29:24
问题 How to solve model.find() function produces "buffering timed out after ... ms"? I'm using mongoose v 5.11.0, npm v6.14.8 and mongodb v Here's the code. var express = require('express'); var app = express(); var bodyParser = require('body-parser'); const assert = require('assert'); var mongoose = require('mongoose'); try { var db = mongoose.connect('mongodb://localhost:27017', {useNewUrlParser: true, dbName: 'swag-shop' }); console.log('success connection'); } catch (error) { console.log(

How to solve Mongoose v5.11.0 model.find() error: Operation `products.find()` buffering timed out after 10000ms"

你说的曾经没有我的故事 提交于 2020-12-15 06:29:08
问题 How to solve model.find() function produces "buffering timed out after ... ms"? I'm using mongoose v 5.11.0, npm v6.14.8 and mongodb v Here's the code. var express = require('express'); var app = express(); var bodyParser = require('body-parser'); const assert = require('assert'); var mongoose = require('mongoose'); try { var db = mongoose.connect('mongodb://localhost:27017', {useNewUrlParser: true, dbName: 'swag-shop' }); console.log('success connection'); } catch (error) { console.log(

Caught an error while pushing a product to cart. TypeError: Cannot read property 'push' of undefined

假装没事ソ 提交于 2020-12-15 05:37:34
问题 I want to push the product details into the cart model when a user clicks on "add to cart" button on a particular product. But it shows TypeError: Cannot read property 'push' of undefined Here is the route file: var express = require("express"), router = express.Router(), Product = require("../models/Product"), Cart = require("../models/Cart"); router.post("/product/:id/addCart", isLoggedIn, function(req, res){ const quantity = req.body.quantity; let cart = new Cart(); Product.findById(req