ArewegoodQ ArewegoodQ - 5 months ago 30
Node.js Question

Document insert with Mongoose

Here's my Mongoose schema:

var DealSchema = new Schema({
deal:{
dealid:{
type: String,
require: true,
unique: true,
},
title: String,
},

// Embedded sub-document
details: {
detail: String,
price: Number // USE: deal.details.price
}
})


My insert statement is as follows:

db.deals.insert({
deal.dealid: '1',
deal.title: 'deal',

details.detail: 'Free Food',
details.price: 200
})


The error I am getting:

SyntaxError: missing : after property id @(shell):2:4


Not sure what this error means - how can I fix it?

Answer

Your object syntax in the insert is incorrect - it needs to be proper JSON, unless you use dot notation as @krl explained.

Assuming you're using Mongoose:

db.deals.insert({
    deal: {
        dealid: '1',
        title: 'deal'
    },
    details: {
        detail: 'Free Food',
        price: 200
    }
});