通过 mongoose 连接到 mongodb 有困难

苏打

这就是我通过猫鼬连接到 mongodb 的方式...

猫鼬连接

当我运行 seeder.js 时,我收到以下错误......无效的架构,预期的 mongodb 或 mongodb +srv。

错误文件

这是我的架构...

模式

这就是我播种数据的方式...

//require the schema here
var Product = require('../models/product');

var mongoose = require('mongoose');
mongoose.connect('localhost:27017/shopping');

//create an array to store new data based on imported schema

var products = [
    
    //creating a new product
    new Product({
    imagePath: 'http://2.bp.blogspot.com/-CEuaUzSjQNc/U9aW9n5xngI/AAAAAAAACmc/mh-994sOx08/s1600/The+C+++Programming+Language+4th+Edition+By+Bjarne+Stroustrup+Pdf+Download.jpg',
    title: 'C++ Bjarne Stroustroup',
    description: 'New C++11 Standard Master Class',
    price: 10
  }) ,

new Product({
    imagePath: 'https://images-na.ssl-images-amazon.com/images/I/41CFs9dkaCL.jpg',
    title: 'C++ in a Nutshell',
    description: 'This book introduces standard C++* and the key programming and design techniques supported by C++. Standard C++ is a far more powerful and polished language than the version of C++ introduced by the first edition of this book. ',
    price: 20
  }),

    new Product({
        imagePath: 'https://images-na.ssl-images-amazon.com/images/I/41jXKNG%2BCZL.jpg',
        title: 'C++ Primer Plus',
        description: 'You will cover the latest and most useful language enhancements, the Standard Template Library and ways to streamline object-oriented programming with C++.',
        price: 25
  }),

    new Product({
        imagePath: 'https://images-na.ssl-images-amazon.com/images/I/91Ln8mm52fL.jpg',
        title: 'Thinking in C++: Introduction to Standard C++, Volume One',
        description: '"This book is a tremendous achievement. You owe it to yourself to have a copy on your shelf. The chapter on iostreams is the most comprehensive and understandable treatment of that subject I\'ve seen to date." ― Al Stevens Contributing Editor, Doctor Dobbs Journal',
        price: 30
  }),
  
  new Product({
    imagePath: 'https://images-na.ssl-images-amazon.com/images/I/51j4ugAKqBL.jpg',
    title: 'Effective Modern C++',
    description: 'Coming to grips with C 11 and C 14 is more than a matter of familiarizing yourself with the features they introduce e.g. auto type declarations move semantics lambda expressions and concurrency support . ',
    price: 17
}),

new Product({
    imagePath: 'https://images-na.ssl-images-amazon.com/images/I/31ENaunM9kL.jpg',
    title: 'Modern C++ Design: Generic Programming and Design Patterns Applied',
    description: 'In Modern C++ Design, Andrei Alexandrescu opens new vistas for C++ programmers. Displaying extraordinary creativity and programming virtuosity, Alexandrescu offers a cutting-edge approach to design that unites design patterns, generic programming and C++, enabling programmers to achieve expressive, flexible and highly reusable code.',
    price: 27
})

];


var done =0;
for(var i=0; i<products.length; i++ ) {
    
    //saving the documents to db via mongoose's save function
    products[i].save( (err, result) => {
        done++;
        if(done===products.length) {
            exit();
        }
    });
}

function exit() {

    mongoose.disconnect();
}

当我从 mongo shell 运行数据库时,它显示一个空的数据库,这意味着播种失败。

苏打

通过 mongoose 连接到 mongodb 遵循以下语法

mongoose.connect("mongodb://hostname:/port/dbname");

在给定的问题中,相同的代码是

mongoose.connect("hostname:/port/dbname");

所以只需在connect方法的url字符串的开头添加mongodb即可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过 Mongoose (nodejs) 从远程服务器连接到 MongoDB

使用Tunnel-ssh通过ssh通过nodeJS中的mongoose通过SSH连接到远程服务器mongoDB

用Mongoose无法连接到MongoDB

无法使用Mongoose连接到MongoDB

流星能否通过SSL连接到MongoDB?

Java中通过SSH连接到MongoDB

通过SSH golang连接到mongoDB

通过MongoStore连接到现有的Mongoose连接-错误:TypeError:无法读取未定义的属性“ Store”

如何使用 mongoose 将我的 socket.io 连接到我现有的 mongodb 连接

MongoDB在docker-compose环境中通过mongoose与nodeJs的连接

如何使用带有证书文件的MongoDB.Driver通过TLS连接到服务器?

动态数据库连接到NodeJS的mongodb或mongoose

我可以将Mongoose连接到分片的MongoDB实例吗?

如何在Mongoose上将MongoDB连接到Heroku?

Dialogflow履行不适用于Mongoose以连接到MongoDB

连接到数据库时,MongoDB Mongoose错误

Mongodb:连接到现有文档

如何通过 swi prolog 连接到 MongoDB 服务器?

通过MongoDB Compass社区连接到EC2

无法通过mongo shell连接到mongodb地图集

如何使用Mac终端通过ssh连接到mongodb

通过终端连接到MongoDB的本地安装时出错

通过节点服务器连接到MongoDB的警告

通过身份验证连接到MongoDB失败

无法通过Docker中的node.js连接到MongoDB

通过VB.NET 2010连接到MongoDB

如何使用MongoDB的graphLookup查询回答问题:给我所有通过酒店连接到“X”的人

使用 Mongoose 的带有 NodeJS 的 MongoDB Atlas 未连接

以响应方式将后端(通过Node.js中Express服务器上的Mongoose通过MongoDB链接到MongoDB)以响应方式链接到前端(Bootstrap或Foundation)?