简体   繁体   中英

mongodb collection inserting record is not working

While inserting the collection in mongo db working with nodejs and mongoose, defined two files

The Model.js

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


var stackSchema = mongoose.Schema({
    name: String
})

var Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

var tagData = new Schema(
{
    tag  : String,
    data :[viewData]
}
    )
var viewData = new Schema({
    view     : String,
    date      : Date
});

module.exports = mongoose.model('Tag', tagData);

The View.js

var Tag =  require('./Model');

var view =({
    "view" : "700",
    "date" : "2001/1/20"

});

var tag = new Tag(
            {"tag" : "java"}


    );


tag.data.push(view);
//console.log(tag)
tag.save(function (err) {
  if (!err) console.log('Success!');

});

While executing the above code i can see the sucess message in the console

bash $ node view.js 
Success!

When i query the mongodb collections it return nothing

> db.stack1.find()

What wrong i'm doing and how can i correct it

That find query is looking in the stack1 collection of the default database in the shell.

To query the contents of the tags collection in the stack1 database that you're targeting with your code:

> use stack1
> db.tags.find()

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