3

How do I get a clean insert without extraneous characters being added when trying to insert an array of object. If I manually do an insert from mongodb shell I get the expected results, otherwise it doesn't seem to work.

What I'm trying to achieve is the results from mongodb shell:

db.test.insert([{name:"john"},{name:"jane"}]);

which yields:

db.test.find()
{ "_id" : ObjectId("53bb0768dc2469c1f440a3c2"), "name" : "john" }
{ "_id" : ObjectId("53bb0768dc2469c1f440a3c3"), "name" : "jane" }

But I don't get that, so I used the code snippet below to test several ways to insert the array of objects hoping to find the right combination:

test = new Meteor.Collection("test");
a = new Array();
a.push({name:"john"});
a.push({name:"jane"});
console.log(a);
test.insert(a);  
console.log(a.toString());
test.insert(a.toString());  
console.log(JSON.stringify(a));
test.insert(JSON.stringify(a));
test.insert([{name:"john"},{name:"jane"}]);
test.insert([{"name":"john"},{"name":"jane"}]);

What I get in the console:

[ { name: 'john' }, { name: 'jane' } ]
[object Object],[object Object]
[{"name":"john"},{"name":"jane"}]

What I get in the database: db.test.find()

{ "0" : { "name" : "john" }, "1" : { "name" : "jane" }, "_id" : "SYkv79XLNQsWgkYmw" }
{ "0" : "[", "1" : "o", "2" : "b", "3" : "j", "4" : "e", "5" : "c", "6" : "t", "7" : " ", "8" : "O", "9" : "b", "10" : "j", "11" : "e", "12" : "c", "13" : "t", "14" : "]", "15" : ",", "16" : "[", "17" : "o", "18" : "b", "19" : "j", "20" : "e", "21" : "c", "22" : "t", "23" : " ", "24" : "O", "25" : "b", "26" : "j", "27" : "e", "28" : "c", "29" : "t", "30" : "]", "_id" : "SiQ3ZpGfeBqj4mXB2" }
{ "0" : "[", "1" : "{", "2" : "\"", "3" : "n", "4" : "a", "5" : "m", "6" : "e", "7" : "\"", "8" : ":", "9" : "\"", "10" : "j", "11" : "o", "12" : "h", "13" : "n", "14" : "\"", "15" : "}", "16" : ",", "17" : "{", "18" : "\"", "19" : "n", "20" : "a", "21" : "m", "22" : "e", "23" : "\"", "24" : ":", "25" : "\"", "26" : "j", "27" : "a", "28" : "n", "29" : "e", "30" : "\"", "31" : "}", "32" : "]", "_id" : "kKRiR8NjNJefBYRya" }
{ "0" : { "name" : "john" }, "1" : { "name" : "jane" }, "_id" : "RBrvkrw5xZaEGdczF" }
{ "0" : { "name" : "john" }, "1" : { "name" : "jane" }, "_id" : "2cfWJqHY4aJ6yF68s" }

I expected a simple 'test.insert(a)' to give me what I want, but it includes the array indexes. How do I build an array of objects to insert into mongodb from meteor without the array indexes? Stringify seemed to build a clean looking serialization of the array, but apparently I just don't know how to do this? The purpose of this is so I can build a complex array of objects in memory and do a bulk insert.

2 Answers 2

1

Meteor only lets you store root level documents as objects, if you give it an array it will try to convert it to an object. This is why you're getting this weird result. You would have to adjust your document to store arrays as part of the root document

test = new Meteor.Collection("test");
a = new Array();
a.push({name:"john"});
a.push({name:"jane"});

var doc = {
    names: a
}

test.insert(a);

It won't be possible to store a document as [].

Sign up to request clarification or add additional context in comments.

6 Comments

I'm not sure I understand. If I do: db.test.insert([{name:"john"},{name:"jane"}]); which is exactly like what I do in code, it works from shell but not from code?
My understanding is that the bulk insert does allow for []. Isn't that a requirement for bulk insert to work?
Oh I see what you were trying to do. Unfortunately meteor doesn't yet support bulk inserts the same way mongo does
Well that's a real bummer. Looks like I found a similar question with a workaround, but not really a good one. Seems like it is so close to being implemented, I wonder what the hold up is. stackoverflow.com/questions/19757434/…
@sday the issue with it is checking whether each one is allowed/denied client side, you have to go through each one anyway, so there's no real point of a bulk insert. Server side you can use something like meteor expose to get you access to the mongodb methods to do a bulk insert directly.
|
0
Template.first.rendered=function(){
    var a=[];
    a.push({name:"rahul"});
    a.push({name:"vidu"});
    Meteor.call("array", a , function(error,result){
    });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.