objectId在mongoose中被更改

时间:2017-03-03 17:24:56

标签: node.js mongodb mongoose

我正在尝试将objectId分配给具有嵌套文档的mongoose模式。但不知何故,当它被保存时,ObjectId会被更改。

review.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var user = require('./user.js');

var reviewSchema = new Schema({
    item_id:String,
    review:[{
        item_review:String,
        user: {type: Schema.Types.ObjectId,ref: 'user'}
    }]
},{ versionKey: false });



module.exports = mongoose.model('review',reviewSchema);

user.js的

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var user = new Schema({
    _id:String,
    login_type:Number,   // 0 :facebook, 1: google
    profile_img:String,
    ph_no:Number,
    email_id:{type:String,required:true}
},{ versionKey: false });


module.exports = mongoose.model('user',user);

reviews.js

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var reviews = require('./../model/review.js');
var user = require('./../model/user.js');


var getReview = function(req,res) {
    reviews
.findOne({'item_id':req.body.item_id.trim()})
.populate('user')
.exec(function (err, review) {
   if (err) res.json({
            status:'400',
            message:'There is no data available'
        });
        res.send({
            status:'200',
            message:'Review List',
            data:review
        });
});
};


var addreview = function(req,res) {
    reviews.findOne({'item_id':req.body.item_id.trim()},function(err,revie){
        if (err) res.json({
                    status:'400',
                    message:'Oops, feedback can not be submitted'
                });
        if(revie){
            revie.review.push({
                item_review:req.body.item_review.trim(),
                user :  mongoose.Types.ObjectId(req.body.user_id)
                // user_id:req.body.user_id.trim(),
                // user_name:req.body.user_name.trim()
            });

            revie.save(function(err,review){
                res.json({
                    status:'200',
                    message:'Thanks for your feedback'
                });
            });
        }
        else {
            var newReview = new reviews();
            newReview.item_id = req.body.item_id.trim();
            newReview.review.push({
                item_review:req.body.item_review.trim(),
                user :  mongoose.Types.ObjectId(req.body.user_id)
            });

            newReview.save(function(err,review){
                res.json({
                    status:'200',
                    message:'Thanks for your feedback'
                });
            });
        }
    });
};


module.exports = {addreview,getReview};

如何在不更改实际ID的情况下连接两个模型?

1 个答案:

答案 0 :(得分:0)

看起来您使用_id作为字符串而不是ObjectId,我建议您将架构更改为此。

var user = new Schema({
    login_type:Number,   // 0 :facebook, 1: google
    profile_img:String,
    ph_no:Number,
    email_id:{type:String,required:true}
},{ versionKey: false });

默认情况下删除_id,并且需要是ObjectId而不是字符串。