MongoDB中实现INSERT AS SELECT

MongoDB中实现INSERT AS SELECT

db.orders.find({ 条件: 1 }).
forEach( function(i) {
i.ts_imported = new Date();
db.orders1.insert(i);
});

db.orders.find().forEach( function(i) {
i.ts_imported = new Date();
db.orders1.insert(i);
});

> db.orders1.find();
{ “_id” : ObjectId(“55706f6f0fbdc5f8cdb3cf57”), “OrderID” : 10248, “CustomerID” : “VINET”, “EmployeeID” : 5, “OrderDate” : “1996-07-04 00:00:00.000”, “RequiredDate” : “1996-08-01 00:00:00.000”, “ShippedDate” : “1996-07-16 00:00:00.000”, “ShipVia” : 3, “Freight” : 32.38, “ShipName” : “Vins et alcools Chevalier”, “ShipAddress” : “59 rue de l’Abbaye”, “ShipCity” : “Reims”, “ShipRegion” : “NULL”, “ShipPostalCode” : 51100, “ShipCountry” : “France”, “ts_imported” : ISODate(“2015-06-04T15:34:43.618Z”) }
{ “_id” : ObjectId(“55706f6f0fbdc5f8cdb3cf58”), “OrderID” : 10249, “CustomerID” : “TOMSP”, “EmployeeID” : 6, “OrderDate” : “1996-07-05 00:00:00.000”, “RequiredDate” : “1996-08-16 00:00:00.000”, “ShippedDate” : “1996-07-10 00:00:00.000”, “ShipVia” : 1, “Freight” : 11.61, “ShipName” : “Toms Spezialitäten”, “ShipAddress” : “Luisenstr. 48”, “ShipCity” : “Münster”, “ShipRegion” : “NULL”, “ShipPostalCode” : 44087, “ShipCountry” : “Germany”, “ts_imported” : ISODate(“2015-06-04T15:34:43.626Z”) }
{ “_id” : ObjectId(“55706f6f0fbdc5f8cdb3cf59”), “OrderID” : 10250, “CustomerID” : “HANAR”, “EmployeeID” : 4, “OrderDate” : “1996-07-08 00:00:00.000”, “RequiredDate” : “1996-08-05 00:00:00.000”, “ShippedDate” : “1996-07-12 00:00:00.000”, “ShipVia” : 2, “Freight” : 65.83, “ShipName” : “Hanari Carnes”, “ShipAddress” : “Rua do Paço”, “ShipCity” : 67, “ShipRegion” : “Rio de Janeiro”, “ShipPostalCode” : “RJ”, “ShipCountry” : “05454-876”, “field14” : “Brazil”, “ts_imported” : ISODate(“2015-06-04T15:34:43.627Z”) }
{ “_id” : ObjectId(“55706f6f0fbdc5f8cdb3cf5a”), “OrderID” : 10251, “CustomerID” : “VICTE”, “EmployeeID” : 3, “OrderDate” : “1996-07-08 00:00:00.000”, “RequiredDate” : “1996-08-05 00:00:00.000”, “ShippedDate” : “1996-07-15 00:00:00.000”, “ShipVia” : 1, “Freight” : 41.34, “ShipName” : “Victuailles en stock”, “ShipAddress” : 2, “ShipCity” : “rue du Commerce”, “ShipRegion” : “Lyon”, “ShipPostalCode” : “NULL”, “ShipCountry” : 69004, “field14” : “France”, “ts_imported” : ISODate(“2015-06-04T15:34:43.629Z”) }

 

若要重复插入原有记录 则需要

 

db.orders.find().forEach( function(i) {
i._id = new ObjectId();
db.orders2.insert(i);
});

Comment

*

沪ICP备14014813号

沪公网安备 31010802001379号