0

I would like to add an element in an array in a mongo database:

db.keypairs.update( {pubkey: "1234567890"}, { $push: {listTxId: {txHash: "yyy", spent: false} } } )

The result is perfect:

listTxId" : [ { "txHash" : "xxx", "spent" : true },{ "txHash" : "yyy", "spent" : false } ]

Now I would like to do the same with node.js and mongoose

var res = wait.forMethod(Keypair,'update', {pubkey: "1234567890"}, { $push: { "listTxId": {"txHash":"zzz", "spent":false} } } );

Keypair is my node.js model for the mongoose collection:

var Keypair = require('./app/models/Keypair');

and wait.forMethod comes from a node module:

var wait = require('wait.for');

In the result, I have this "_id" element :

{ "txHash" : "zzz", "spent" : false, "_id" : ObjectId("56561571fea5d9a10a5771fd") }

QUESTION: where this ObjectId come from ? How can I get rid of it ?

UPDATE: mongoose schema:

var keypairSchema = mongoose.Schema({
    userId      : { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    pubkey      : String,
    privkeyWIF  : String, // temp
    balance     : Number,
    listTxId    : [{
        txHash : String,
        spent  : Boolean
     }],
    walletId    : { type: mongoose.Schema.Types.ObjectId, ref: 'Wallet' },
    description : { type: String, maxlength: 40 },
    comments    : String,
    isMasterKey : { type: Boolean, default: false },
    date        : Date
});
jfjobidon
  • 327
  • 5
  • 18

1 Answers1

1

Mongoose will put ids in your subdocument arrays. listTxId is a subdocument array. You can add _id: false to your schema to prevent this:

var keypairSchema = mongoose.Schema({
    userId      : { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    pubkey      : String,
    privkeyWIF  : String, // temp
    balance     : Number,
    listTxId    : [{
        _id: false,
        txHash : String,
        spent  : Boolean
     }],
    walletId    : { type: mongoose.Schema.Types.ObjectId, ref: 'Wallet' },
    description : { type: String, maxlength: 40 },
    comments    : String,
    isMasterKey : { type: Boolean, default: false },
    date        : Date
});
katy lavallee
  • 2,741
  • 1
  • 28
  • 26