简体   繁体   English

从流星中的对象数组插入集合中的文档

[英]Insert documents in collection from array of objects in Meteor

I have an array dataRows consisting of multiple objects, eg 我有一个数组dataRows由多个对象组成,例如

{
  'The Name': 'John',
  'My Age': 44,
  'My Address': 'Some street',
  [...]
}

I want to insert these objects into a collection. 我想将这些对象插入集合中。

Since I could just use MyCollection.insert( object ) , I guess I can just insert the objects directly into a collection; 因为我可以只使用MyCollection.insert( object ) ,所以我想我可以将对象直接插入到集合中。 however, the problem is that the keys in my objects are differently named than the field names in my collection and I don't want to insert all of the fields in my collection. 但是,问题在于,对象中的键的名称与集合中的字段名称不同,并且我不想在集合中插入所有字段。

I could just do this: 我可以这样做:

dataRows.forEach( function ( row ) {
  MyCollection.insert( {
    name: row['The Name'],
    age: row['My Age'],
    address: row['My Address'],
  } );
} )

but I think it would be better if I could manipulate the array dataRows directly. 但是我认为直接操作数组dataRows会更好。

Besides, I don't want to skip fields with empty values. 此外,我不想跳过带有空值的字段。

Edit 编辑

I have an array 我有一个数组

[
  {
    'The Name': 'John',
    'My Age': 44,
    'My Address': 'Some street',
    [...]
  },
  {
    'The Name': 'Michael',
    'My Age': 57,
    'My Address': '',
    [...]
  },
  {
    'The Name': 'Anne',
    'My Age': 31,
    'My Address': 'Some other street',
    [...]
  },
  [...]
]

I want to manipulate the array, so the keys can be renamed (eg The Name should be name , My Age should be age , etc.), fields with no value should be removed (eg the second object in the array has a key My Address with an empty value. The key should be removed from this object), and I want which keys should be kept and the rest of the keys should be remove (eg all objects in the array should have fields name , age , address and all other fields should be removed from each object. 我想操作数组,因此可以重命名键(例如The Name应为nameMy Age应为age等),应删除无值的字段(例如,数组中的第二个对象具有键My Address为空的值。应从该对象中删除键),我想保留哪些键,其余键应删除(例如,数组中的所有对象都应具有nameageaddress和所有字段其他字段应从每个对象中删除。

I guess I should use array.map() , array.filter() , etc. 我猜我应该使用array.map()array.filter()等。

I hope it clarifies my question. 我希望它能澄清我的问题。

You can still manipulate the array using the forEach() method and within the loop take advantage of using a write commands Bulk API that allows for the execution of bulk insert operations which are simply abstractions on top of the server to make it easy to build bulk operations, thus streamlining your inserts . 您仍然可以使用forEach()方法操作该数组,并且在循环内利用写命令Bulk API优势,该Bulk API允许执行批量插入操作,这些操作只是服务器顶部的抽象,从而易于构建批量操作,从而简化了inserts These bulk operations come mainly in two flavours: 这些批量操作主要有两个方面:

  • Ordered bulk operations . 有序批量操作 These operations execute all the operation in order and error out on the first write error. 这些操作按顺序执行所有操作,并在第一个写入错误时出错。
  • Unordered bulk operations . 无序批量操作 These operations execute all the operations in parallel and aggregates up all the errors. 这些操作并行执行所有操作,并汇总所有错误。 Unordered bulk operations do not guarantee order of execution. 无序批量操作不能保证执行顺序。

You can get raw access to the collection and database objects in the npm MongoDB driver through rawCollection and rawDatabase methods on Mongo.Collection . 你可以在通过NPM MongoDB的驱动程序收集和数据库对象的原始访问rawCollectionrawDatabase上的方法Mongo.Collection For example, manipulate the array using forEach() , construct the object to be inserted within the loop, send the insert operations in batches for improved write perfomances as follows: 例如,使用forEach()操作数组,构造要在循环中插入的对象,分批发送插入操作以提高写入性能,如下所示:

MyCollection = new Meteor.Collection("mycollection");

if (Meteor.isServer) {
    Meteor.startup(function () {
        Meteor.methods({
            insertData: function() {
                var bulkOp = MyCollection.rawCollection().initializeUnorderedBulkOp(),
                    counter = 0,
                    dataRows = [...]; // the raw array

                dataRows.forEach(function(row) {
                    var data = {
                        name: row['The Name'],
                        age: row['My Age'],
                        address: row['My Address'],
                    };
                    bulkOp.insert(data);

                    counter++;
                    if (counter % 1000 == 0) {
                        // Execute per 1000 operations and re-initialize every 1000 update statements
                        bulkOp.execute(function(e, rresult) {
                            // do something with result
                        });
                        bulkOp = MyCollection.rawCollection().initializeUnorderedBulkOp();
                    }
                }); 

                // Clean up queues
                if (counter % 1000 != 0){
                    bulkOp.execute(function(e, result) {
                        // do something with result
                    });
                }
            }
        }); 
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM