mysql insert as select_MongoDB中实现INSERT AS SELECT

01fac6460a22031b8af8da5848698a70.png

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);

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值