简体   繁体   中英

Mongodb does not save a document

I am trying to store some data from an HTML formulary. I send the data using the HTTP POST method and I received them using Express framework in Node.js. The data arrives and it seems to work, but when I try to store them into MongoDB using Mongoose, the database is created but no data is stored when I execute DB.sis_dictionary.find()

I've tried to build different types of schemas and models, but none seems to work. And I get no error from Node.js, it seems to be working, but the MongoDB database does not store anything.

const Mongoose = require('mongoose');
Mongoose.connect('mongodb://localhost:27017/sis_dictionary', {useNewUrlParser: true});
const Schema = Mongoose.Schema;
const wordSchema = new Schema({
  word: String
})
const Word = Mongoose.model('Word', wordSchema);

app.post('/saveWord', (req, res) => {
    var word = new Word({word: String(req.body)});
    word.save(function(err){
      if(err) {
        return console.error(err);
      } else {
        console.log("STATUS: WORKING");
      }
    })
    console.log(req.body);
})

server.listen(3000);

console.log("SERVER STARTUP SUCCESS");

In the console, I get the message: "STATUS: WORKING".

sis_ditionary is your DB name and Words should be your collection name. As mongoose automatically creates a plural name for collection from a model if model name not specified when creating from a schema

db.collection.find() is a command to find a collection data when using mongo-shell. Run below command to get data:

  • use sis_dictionary

  • db.Words.find()

    To beautify result use pretty method

    db.Words.find().pretty()

First command will select DB and second command list collection data.

So when you execute db.sis_dictionary.find() it won't work because sis_dictinary is your DB name.

Nodejs way with 'mongoose'

//Model.find({});
  Word.find({});

Also, check this line var word = new Word({word: String(req.body)}); What does req.body have? If req.body is {word:"example word"} then you directly pass req.body to modal constructor ie new Word(req.body) ;

According to your database URL, mongodb://localhost:27017/sis_dictionary , sis_dictionary is the database name.

And according to your mongoose model, Word is your collection name.

When you save a document, it saves under a collection. So you have to make a query under the collections.

So when you try to get data using DB.sis_dictionary.find() , definitely it won't work.

Your query should be like db.collection.find()

Use the following query,

use sis_dictionary
db.words.find()
// for better view
db.words.find().pretty()

For more please check the documentation .

Thank you everybody. You were all right, it was a problem related to my collections names. db.words.find().pretty() worked perfectly.The problem is solved.

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