如何使用meteor将对象数组插入mongodb? [英] how do I insert an array of objects to mongodb using meteor?

查看:62
本文介绍了如何使用meteor将对象数组插入mongodb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试插入对象数组时,如何在不添加无关字符的情况下获得干净的插入.如果我从 mongodb shell 手动执行插入,我会得到预期的结果,否则它似乎不起作用.

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.

我想要实现的是来自 mongodb shell 的结果:

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

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

产生:

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

我在控制台中得到的:

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

我在数据库中得到了什么:db.test.find()

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" }

我期待一个简单的test.insert(a)"来给我我想要的东西,但它包括数组索引.如何在没有数组索引的情况下构建要从流星插入 mongodb 的对象数组?Stringify 似乎构建了一个干净的数组序列化,但显然我只是不知道如何做到这一点?这样做的目的是让我可以在内存中构建一个复杂的对象数组并进行批量插入.

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.

推荐答案

Meteor 只允许你将根级文档存储为对象,如果你给它一个数组,它会尝试将它转换为一个对象.这就是为什么你会得到这个奇怪的结果.您必须调整您的文档以将数组存储为根文档的一部分

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 [].

这篇关于如何使用meteor将对象数组插入mongodb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆