简体   繁体   中英

can't find mongodb collections/data

I'm working on a web app with node/express/mongodb, using mongoose. I created a folder in my project called data and inside I created another folder called db

  • data
    • db

when I start mongo, I use the --dbpath parameter and point it to my project data folder that I created. It connects fine and everything. I've been writing some data to the database, and the application loads it correctly, but I can't find the data when I open mongo shell.

first thing is, when I open shell the only database I get is test . Is this auto created if I don't specify a database name? how do I specify a database name? I have the following in my app.js file:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/abduldb');

My guess is that a database named abduldb should be created correct? I don't see this in mongo shell.

Also, when using the test database, I run show collections and it shows no results... Where exactly is my data being written? I only have 1 model right now and it looks like this:

var mongoose = require('mongoose');

var ItemSchema = new mongoose.Schema({
  title: String,
  body: String
});

mongoose.model('item', ItemSchema);

and I'm adding new items in my route file using:

router.post('/items', function (req, res, next) {
  var item = new Item(req.body);
  item.save(function(err, item){
    if(err){ return next(err); }
    res.json(item);
  });
});

When you are in the mongo shell, try this command: "use abduldb".

The reason you don't see your collections is because you are automatically connected to the test db. If you want to change that, run this when you start mongo shell:

mongo abduldb

Alternatively, you can change the default db in your .mongorc.js file by adding/changing this line:

db = db.getSiblingDB("abduldb")

The .mongorc.js file can be found at /etc/mongorc.js, but is overridden if it exists in your home directory (ie /home/abduldb/.mongorc.js).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM