4

I want make primary key no need to input but primary key auto generate in mongodb. so, i use {type : ObjectId,required:false}, but it wont work because I let the primary key empty. so is there another ways to make pprimary key optional to input? Thankyou

rest api model

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId

var accessmenu = new Schema({
   _id : {type : ObjectId,required: false},
   acc_id : String,
   name : String,
   read : Boolean,
   execute : Boolean
},{ collection: 'access_menu'});

var accessmenu = mongoose.model("accessmenu",accessmenu);
module.exports.accessmenu = accessmenu;

rest api

app.put("/access_menu/:id",function(req,res){
    var AccessMenu = new accessmenu({
        _id : req.body._id,
        acc_id : req.body.acc_id,
        name : req.body.name,
        read : req.body.read,
        execute : req.body.execute
    });
    AccessMenu.save(function(err){
        if(err)
        {
            accessmenu.update({_id : req.params.id},{$set:{acc_id : req.body.acc_id,
                name : req.body.name,
                read : req.body.read,
                execute : req.body.execute
                }},function(err,users){
                if(err)
                {
                    data['error'] = 1;
                    data['Access_Menu'] = "update faill";
                    res.json(data);
                }
                else
                {
                    data['error'] = 0;
                    data['Access_Menu'] = "update success";
                    res.json(data);
                }
            });
        }
        else
        {
            data['error'] = 0;
            data['Access_Menu'] = "input success";
            res.json(data);
        }
    });
}); 

script.js

if($scope.data_acc_lv.length > 0)
    {
        for(var i = 0;i<$scope.data_acc_lv.length;i++)
        {
            var input3 = {
                "_id" : $scope.data_acc_lv[i]._id,
                "acc_id":$scope.accLvID,
                "name": $scope.data_acc_lv[i].name,
                "read": $scope.data_acc_lv[i].read,
                "execute": $scope.data_acc_lv[i].execute
            }
            $http.put("http://localhost:22345/access_menu/" + $scope.data_acc_lv[i]._id,input3)
            .success(function(res,err){
                if(res.error == 0)
                {
                    $scope.data_acc_lv.length = 0;
                }
                else
                {
                    console.log(err);
                }

            }); 
        } 
    }
Jan sebastian
  • 308
  • 1
  • 4
  • 18

1 Answers1

3

Mongoose assigns each of your schemas an _id field by default if one is not passed into the Schema constructor. The type assigned is an ObjectId to coincide with MongoDB's default behavior.

If you don't want an _id added to your child schema at all, you may disable it using this option.

// disabled _id
var childSchema = new Schema({ name: String }, { _id: false });
var parentSchema = new Schema({ children: [childSchema] });

You can only use this option on sub-documents. Mongoose can't save a document without knowing its id, so you will get an error if you try to save a document without an _id.

http://mongoosejs.com/docs/guide.html

Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34