简体   繁体   English

mongodb集合插入记录不起作用

[英]mongodb collection inserting record is not working

While inserting the collection in mongo db working with nodejs and mongoose, defined two files 在将集合插入到使用nodejs和mongoose的mongo db中时,定义了两个文件

The Model.js 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 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 当我查询mongodb集合时,它什么也不返回

> 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. find查询正在外壳程序中的默认数据库的stack1集合中查找。

To query the contents of the tags collection in the stack1 database that you're targeting with your code: 要在您的代码所针对的stack1数据库中查询tags集合的内容,请执行以下操作:

> use stack1
> db.tags.find()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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