首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >很难通过猫鼬连接mongodb

很难通过猫鼬连接mongodb
EN

Stack Overflow用户
提问于 2018-05-20 07:07:45
回答 1查看 310关注 0票数 0

我就是这样通过猫鼬连接到mongodb的..。

当我运行seeder.js时,我会得到以下错误.无效模式,预期的mongodb或mongodb +srv。

这是我的计划..。

我就是这样播种数据的..。

代码语言:javascript
复制
//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运行db时,它会显示一个空数据库,这意味着种子正在失败。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-20 07:07:45

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

代码语言:javascript
复制
mongoose.connect("mongodb://hostname:/port/dbname");

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

代码语言:javascript
复制
mongoose.connect("hostname:/port/dbname");

因此,只需在连接方法中url字符串的开头添加mongodb即可。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50432501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档