impossible
impossible

Reputation: 2500

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?

Upvotes: 2

Views: 2578

Answers (3)

Phillip Elm
Phillip Elm

Reputation: 2194

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
    }
});

Upvotes: 2

krl
krl

Reputation: 5296

To specify or access a field of an embedded document with dot notation, concatenate the embedded document name with the dot (.) and the field name, and enclose in quotes (both single and double quotes are acceptable) (see Documents > Dot Notation > Embedded Documents):

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

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

Upvotes: 3

Tanrikut
Tanrikut

Reputation: 548

JSON format in that call is invalid. Try this:

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

    details: {
           detail: 'Free Food',
           price: 200
    }
});

Upvotes: 1

Related Questions