Mongodb:无法通过命令

时间:2016-08-24 19:46:20

标签: node.js mongodb mongoose mongodb-query mongoose-schema

对于 mongodb的嵌入式文档,我不知道为什么数据未保存在数据库中或其他可能是错误的?我试图打印出所有内容以确保它一直工作到最后一步。但是在查询嵌入式文档时仍然没有任何结果,如下所示。

我的架构:

// create competitorAnalysisSchema
var CompetitorAnalysis = new Schema({
    firstObservation: { type: String },
    secondObservation: { type: String },
    thirdObservation: { type: String },
    brandName: { type: String },
    productCategory: { type: String },
    photo1: { data: Buffer, contentType: String },
    photo2: { data: Buffer, contentType: String },
    photo3: { data: Buffer, contentType: String },
    photo4: { data: Buffer, contentType: String }
});

// create UserSchema
var UserSchema = new Schema({
    userName: { type: String, required: true, unique: true },
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    currentDemo: { type: String },
    nextDemo: { type: String },
    startTime: { type: String },
    startLocation: { type: String },
    arriveTime: { type: String },
    arriveLocation: { type: String },
    leaveTime: { type: String },
    leaveLocation: { type: String },
    competitorAnalysis: [CompetitorAnalysis],
    created_at: Date,
    updated_at: Date
});

var User = mongoose.model('User', UserSchema);
module.exports = User;

在我的index.js中,所有调试消息都可以成功打印出来。:

// on routes that end in /users/competitorAnalysisTextData
// ----------------------------------------------------
router.route('/users/competitorAnalysisTextData/:userName')

    // update the user info (accessed at PUT http://localhost:8080/api/users/competitorAnalysisTextData)
    .put(function(req, res) {

        // use our user model to find the user we want
        User.findOne({ userName: req.params.userName}, function(err, user) {

            if (err)
                res.send(err);
            console.log('Got the user!');

            // update the text data
            user.competitorAnalysis.firstObservation = req.body.firstObservation;
            user.competitorAnalysis.secondObservation = req.body.secondObservation;
            user.competitorAnalysis.thirdObservation = req.body.thirdObservation;
            user.competitorAnalysis.brandName = req.body.brandName;
            user.competitorAnalysis.productCategory = req.body.productCategory;
            console.log('req.body.firstObservation: %s', req.body.firstObservation);
            console.log('user.competitorAnalysis.firstObservation: %s', user.competitorAnalysis.firstObservation);
            console.log('Save the text data for competitorAnalysisTextData!');

            // save the user
            user.save(function(err) {
                if (err)
                    res.send(err);

                res.json({ message: 'User updated!' });
                console.log('user.competitorAnalysis.firstObservation: %s', user.competitorAnalysis.firstObservation);
                console.log('Finally save the User!');
            });

        });
    })

在控制台中:

Got the user in "Put"!
req.body.firstObservation: 3
user.competitorAnalysis.firstObservation: 3
Save the text data for competitorAnalysisTextData!
user.competitorAnalysis.firstObservation: 3
Finally save the User!

问题

但是,当我在我的mongodb数据库中搜索时,没有为嵌入文档保存数据:

...
"leaveTime" : "Your Current Time:\n 2016-08-23 10:27:45 AM",
"leaveLocation" : "Your Current Address:\n 1\nInfinite Loop\nCupertino\n95014",
"competitorAnalysis" : [ ]
}
> db.users.find({"competitorAnalysis.firstObservation" : "3"}).pretty()
> 

在这里空了!

我是mongodb的新手。如果我能得到一些关于我可以检查的地方或问题可能是什么的提示,那就太棒了。

更新

收集的输出:

> db.users.find().pretty()
{
    "_id" : ObjectId("57ba5f41ad8858305a5d3e58"),
    "created_at" : ISODate("2016-08-22T02:11:13.968Z"),
    "updated_at" : ISODate("2016-08-24T19:42:56.311Z"),
    "nextDemo" : "12:00pm - 3:00pm, Whole Foods Market, 5880 Centre Ave, Pittsburgh PA 15206",
    "currentDemo" : "9:00am - 1:00pm,  Whole Foods Market, 5880 Centre Ave, Pittsburgh PA 15206",
    "password" : "<3da4dafc c96e05cd 855da8b3 ff0bf074 8156ec4b b9f1a002 ba907bcc d5e4aa5b fcd2fef9 dec240cd 86489978 7d85cec8 f11eae1c 7b60b2cc 6693da1a 4eae3a73>",
    "email" : "chenya@gmail.com",
    "userName" : "Chenya",
    "__v" : 1,
    "startLocation" : "Your Current Address:\n 10141\nBilich Pl\nCupertino\n95014",
    "startTime" : "Your Current Time:\n 2016-08-24 03:42:42 PM",
    "arriveTime" : "Your Arriving Time:\n 2016-08-24 03:42:44 PM",
    "arriveLocation" : "Your Arriving Address:\n 10131\nBilich Pl\nCupertino\n95014",
    "leaveTime" : "Your Current Time:\n 2016-08-23 10:27:45 AM",
    "leaveLocation" : "Your Current Address:\n 1\nInfinite Loop\nCupertino\n95014",
    "competitorAnalysis" : [ ]
}
> 

1 个答案:

答案 0 :(得分:1)

这些陈述是问题所在:

    user.competitorAnalysis.firstObservation = req.body.firstObservation;
    user.competitorAnalysis.secondObservation = req.body.secondObservation;
    user.competitorAnalysis.thirdObservation = req.body.thirdObservation;
    user.competitorAnalysis.brandName = req.body.brandName;
    user.competitorAnalysis.productCategory = req.body.productCategory;

您将竞争对手的分析数组视为对象。

我不和Mongoose合作,所以不知道语法,但你想做这样的事情:

user.competitorAnalysis.push({
    firstObservation: req.body.firstObservation,
    secondObservation: req.body.secondObservation,
    thirdObservation: req.body.thirdObservation,
    brandName: req.body.brandName
    productCategory: req.body.productCategory
});