簡體   English   中英

mongodb檢索數據並插入到不同的集合nodejs

[英]mongodb retrieve data and insert to different collection nodejs

我正在嘗試插入/復制? 從a到b的數據。

例如:

var styles = db.collection("style");

 this.getStylesByA = function(a, callback) {
    "use strict";
    styles.findOne({'a': a}, function(err, style) {
        "use strict";

        if (err) return callback(err, null);
        console.log(style);
        callback(err, style);
    });
}

這會給

"_id":_id,
"style":"12345",
"a":"a",
"price":1.00,
"desc":"asldkfjea",
"img":"http://",
"imgs":["http:/","http:/"],
"category":"top",
"colors":[black, white]

這樣的事情。

我想做的是,從樣式集合中查找數據,然后將相同的數據插入到產品集合中,這是一個不同的集合。

先感謝您!

您可以使用一個單獨的集合對象來實現。

var styles = db.collection("style");
var  products =   db.collection("products");

this.getStylesByA = function(a, callback) {

    "use strict";
    styles.findOne({'a': a}, function(err, style) {
        "use strict";
        if (err) return callback(err, null);
            console.log(style);
        //insert style object to products collection
        products.insert(style);

        callback(err, style);
    });

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM