MONGO insert syntaxe

Hello,
I want to make an insert in a Mongo collection.
This insert contains an object table, ‘tva_fac’:
Here is this insert:

Tva.insert({
    "tva_abo_id": "000456",
    "tva_dateOblig": new Date(2017,2,26),
    "tva_dateregl": new Date(2017,2,26),
    "tva_fac": new Array(
        {'numero':'1234765','client':'jean BONNOT - MAIF','montant':39.22,'mntTva':6.43},
        {'numero':'1234766','client':'Leslie PANCUIR - MAAF','montant':51.43,'mntTva':8.43},
        {'numero':'1234767','client':'Ema TITGOUTTE - SECU','montant':169.26,'mntTva':26.26})
})

Why does this ‘insert’ not work ???
In fact the insert is done but the array of object “tva_fac” is empty. I have tried many syntaxes, but nothing does.

Can anyone get me out of this problem?
Thank you
YC

Tva.insert({
    "tva_abo_id": "000456",
    "tva_dateOblig": new Date(2017,2,26),
    "tva_dateregl": new Date(2017,2,26),
    "tva_fac": [
        {'numero':'1234765','client':'jean BONNOT - MAIF','montant':39.22,'mntTva':6.43},
        {'numero':'1234766','client':'Leslie PANCUIR - MAAF','montant':51.43,'mntTva':8.43},
        {'numero':'1234767','client':'Ema TITGOUTTE - SECU','montant':169.26,'mntTva':26.26}
    ]
})

Thank you Rob Fallows but this syntax I used at the beginning and it does not work !!!

Thank you jalligator, there are other surprises of this style in Mongo ???
Well I’ll go deeper ‘$ addToSet’.
I would publish if it works.

Thank you both.
YC

That syntax will definitely work. Perhaps you could post more code?

Ok here are the meteor files

**file shDetailTva.js**
shDetailTVA = new SimpleSchema({
    "numero": String,
    "client": {type: String, optional: true},
    "montant": Number,
    "mntTva": Number,
})
**file shTva.js**
import SimpleSchema from 'simpl-schema';
Tva =new Mongo.Collection('tva',{idGeneration: 'MONGO'});
Tva.attachSchema(new SimpleSchema({
    "tva_abo_id": {     // numero identifiant de l'abonné
        type:String,    // 999999
        index:1
    },
    "tva_dateOblig": Date,
    "tva_dateregl":  Date,
    "tva_fac":     {type:Array},
    "tva_fac.$.detail":  {type:shDetailTVA,optional:true}
}));
**file fixtureTva.js**
if (Tva.find().count() === 0) {
    Tva.insert({
        "tva_abo_id": "000456",
        "tva_dateOblig": new Date(2017,2,26),
        "tva_dateregl": new Date(2017,2,26),
        "tva_fac": [
            {'numero':'1234765','client':'jean BONNOT - MAIF','montant':39.22,'mntTva':6.43},
            {'numero':'1234766','client':'Leslie PANCUIR - MAAF','montant':51.43,'mntTva':8.43},
            {'numero':'1234767','client':'Ema TITGOUTTE - SECU','montant':169.26,'mntTva':26.26}]
    })
};

And the server console meteor after removing the ‘tva’ collection and a new
launch of meteor

meteor:PRIMARY> db.tva.find().pretty()
{
	"_id" : ObjectId("6cdb86517fd2ae64ebfc9d3d"),
	"tva_abo_id" : "000456",
	"tva_dateOblig" : ISODate("2017-03-25T23:00:00Z"),
	"tva_dateregl" : ISODate("2017-03-25T23:00:00Z"),
	"tva_fac" : [ ]
}
meteor:PRIMARY>

Another test,
If I do the same operation directly in the mongodb console, it works perfectly

meteor:PRIMARY> db.tva.insert({"tva_abo_id": "000456",".... same insert as under meteor

{
	"_id" : ObjectId("58c72382b9bfbf25cf9de608"),
	"tva_abo_id" : "000456",
	"tva_dateOblig" : ISODate("2017-03-25T23:00:00Z"),
	"tva_dateregl" : ISODate("2017-03-25T23:00:00Z"),
	"tva_fac" : [
		{
			"numero" : "1234765",
			"client" : "jean BONNOT - MAIF",
			"montant" : 39.22,
			"mntTva" : 6.43
		},
		{
			"numero" : "1234766",
			"client" : "Leslie PANCUIR - MAAF",
			"montant" : 51.43,
			"mntTva" : 8.43
		},
		{
			"numero" : "1234767",
			"client" : "Ema TITGOUTTE - SECU",
			"montant" : 169.26,
			"mntTva" : 26.26
		}
	]
}
meteor:PRIMARY>

So the problem seems to be on the meteor side?

Hello,
I think I have a question
In the definition of the schema, I should have

import SimpleSchema from 'simpl-schema';
Tva =new Mongo.Collection('tva',{idGeneration: 'MONGO'});
Tva.attachSchema(new SimpleSchema({
    "tva_abo_id": {     // numero identifiant de l'abonné
        type:String,    // 999999
        index:1
    },
    "tva_dateOblig": Date,
    "tva_dateregl":  Date,
    "tva_fac":     {type: [Object]},
    "tva_fac.$.detail":  {type:shDetailTVA,optional:true}
}));

With the definition of an object array and not of a single array "‘tva_fac’: {type: [Object]}

But I have the following error on the mongo console :
Error: Invalid definition for tva_fac field: "type" may not be an array. Change it to Array.

How to solve this problem
Merci
YC

I think you’ve answered your own question. I think you’re using the aldeed:collection2 package as well, which “cleans” your data by removing objects if they are not in the schema (which is why tva_fac is not saved).

So you should either bypass cleaning, or ensure the schema matches the data.

Are you using aldeed:simple-schema from atmosphere, or the simpl-schema npm package?

OK, it works
Here is the solution (my solution)

Indeed, the schema must define an object array.
"'Tva_fac": [Object] … "
But this definition generates an error.
In order that there be no error, this declaration must be made in the definition of collection.

Tva =new Mongo.Collection('tva',{idGeneration: 'MONGO', schema:{
    "tva_abo_id": {     // numero identifiant de l'abonné
        type:String,    // 999999
        index:1
    },
    "tva_dateOblig": Date,
    "tva_dateregl":  Date,
    "tva_fac":     [Object],
    "tva_fac.$.detail":  {type:shDetailTVA,optional:true}
}});

Thank you for your answers
YC

1 Like

I use nom

Meteor npm install --save simpl-schema
Meteor add aldeed: schema-index