【问题标题】:How to insert in a collection when other collections already exists当其他集合已经存在时如何插入集合
【发布时间】:2016-06-17 17:22:31
【问题描述】:

我对 noSQL 数据库非常陌生。我已经开始了我在 noSQL 中使用 mongodb 数据库的旅程。直到今天我还在使用 mySQL 和 MS SQL Server。我知道如何创建表格和它们之间的关系。但这个 noSQL 概念与此完全不同。所以,我在问这个问题。我希望我能早点学会使用 noSQL 数据库。

我在 mongodb 中使用了以下查询来创建集合并在其中插入数据:

db.createCollection('Parties');
db.parties.insert({partyName:"Best Sellers", mobileNo:"9876543214"});
db.parties.insert({partyName:"National Traders", mobileNo:"9876543215"});
db.createCollection('items');
db.items.insert({itemName:"Item-A", size:"12"});
db.items.insert({itemName:"Item-B", size:"8"});
db.createCollection('orders');

从视觉上看,我的订单将包含以下字段:

Order No     : AB/123
Date of Order: 17-06-2016
Party Name   : National Traders // It will be a select

Item Name           Quantity        Rate         Amount
--------------------------------------------------------
Item-A //Select        200           20           4000
Item-B //Select        100           30           3000
--------------------------------------------------------
                       300                        7000              

我的问题是:

如何查询 mongodb 以插入订单集合?我从未使用过嵌套集合之类的东西。任何人都可以给我一个例子吗??

更新:

这是我的关系数据库中的数据库图:

如何将其转换为 mongodb 样式??

更新2:

我已尝试在 Node.js 中为模型顺序添加此代码:

var mongoose = require('mongoose');

var orderSchema = new mongoose.Schema({
  salesOrderId: String,
  orderDate: Date,
  party: {type: Schema.Types.ObjectId, ref:'Party'},
  items:[{{type: Schema.Types.ObjectId, ref:'Item'}, quantity: Number, rate: Number}],
  dispatches:{
    invoiceId: Number,
    dispatchDate: Date,
    items: [{{type: Schema.Types.ObjectId, ref:'Item'}, quantity: Number, rate: Number}]
  }
});

mongoose.model('Order', orderSchema);

【问题讨论】:

  • party-items-order 的关系是什么——你能解释一下吗?
  • @profesor79 请查看更新后的问题。
  • 今晚会做。架构看起来很完美!
  • @profesor79 感谢您的回复。

标签: mongodb nosql


【解决方案1】:

MongoDB 不遵循关系数据库数据结构。 将 SQL 数据结构复制到 MongoDB 并不是使用 MongoDB 的理想方式。应修改您的应用程序以使用 MongoDB 风格的数据结构。

为了存储订单数据,我建议只使用一个包含整个订单数据的集合。

{ 
   orderNumber : 7283,
   party : {partyName:"Best Sellers", mobileNo:"9876543214"},
   items : [
       {itemName:"Item-A", size:"12"},
       {itemName:"Item-B", size:"8"},
       .....
   ]
   orderDate : <>,
   total : <>,
   status : <>,
   .....
}

【讨论】:

    【解决方案2】:

    这个问题需要三个集合:

    1. 项目 - 将保存项目数据
    2. 派对 - 将保存派对数据
    3. 将保存所有相关数据的订单

    当方下订单时,在订单集合中,我们正在创建文档并在其中放置一些子文档:

    1. Order 数据(ID、日期等)
    2. PartyDetails - 来自party 集合的整个文档
    3. Items - 包含由当事人选择的所有项目的数组以及详细信息(数量、费率等)
    4. DispatchDispatchDetails - 这取决于物品的运输方式

    这种结构实用高效的面向文档的存储,每次您需要检查订单状态时,您只需要请求一个文档,您就会一次获得子文档。

    在此类集合中插入数据将取决于您将使用的应用程序语言,但我认为这个答案将为您提供文档数据库解决方案的强大功能。

    欢迎任何 cmets!

    * 编辑 *

    var itemToInsertA = db.items.find({"itemName" : "Item-A"}).toArray()
    var itemToInsertB = db.items.find({"itemName" : "Item-B"}).toArray()
    var party = db.parties.find({"partyName" : "National Traders"}).toArray()
    
    var order = {
        //_id - will be assigned by db - but we can do it mannually
        orderDate : new Date(), // now
        salesOrderId : "Need to provide this from sale now or later",
        PartyDetails : party[0],
        Items : [{
                item : itemToInsertA[0],
                Quantity : 200,
                Rate : 20,
                ValueAtTransactionDate : 4000
            }, {
                item : itemToInsertB[0],
                Quantity : 100,
                Rate : 30,
                ValueAtTransactionDate : 3000
            }
        ]
    }
    
    db.orders.insertOne(order)
    

    所以最后我们的订单看起来像这样:

    {
        "_id" : ObjectId("5767d030ecf8248c30af068c"),
        "orderDate" : ISODate("2016-06-20T11:14:56.372Z"),
        "salesOrderId" : "Need to provide this from sale now or later",
        "PartyDetails" : {
            "_id" : ObjectId("5767ce26ecf8248c30af0686"),
            "partyName" : "National Traders",
            "mobileNo" : "9876543215"
        },
        "Items" : [ 
            {
                "item" : {
                    "_id" : ObjectId("5767ce26ecf8248c30af0687"),
                    "itemName" : "Item-A",
                    "size" : "12"
                },
                "Quantity" : 200.0,
                "Rate" : 20.0,
                "ValueAtTransactionDate" : 4000.0
            }, 
            {
                "item" : {
                    "_id" : ObjectId("5767ce26ecf8248c30af0688"),
                    "itemName" : "Item-B",
                    "size" : "8"
                },
                "Quantity" : 100.0,
                "Rate" : 30.0,
                "ValueAtTransactionDate" : 3000.0
            }
        ]
    }
    

    为了创建一个图表,我使用带有 QEditor 的 plantUML。

    植物转储

    @startuml
    
    package "Party"{
    [PartyId]
    [PartyCode]
    [PartyName]
    [MobileNumber]
    }
    
    package "Item"{
    [ItemId]
    [ItemCode]
    [ItemName]
    }
    
    package "Order"{
    [OrderId]
    [SalesOrderId]
    [OrderDate]
    
    
    
    node "Dispatch"{
    [InvoiceId]
    [SalesInvoiceId]
    [DateOfDispatch]
    
    }
    
    node "DispatchItemTransaction"{
    [DispatchItemTId]
    [Quantity.]
    [Rate.]
    [ItemId.]
    [InvoiceId.]
    }
    
    component PartyDetails
    node "Items"{
    component ItemDetails
    [Quantity]
    [Rate]
    [Value]
    }
    
    
    }
    
    
    Item -> ItemDetails
    ItemDetails-->ItemId.
    Party -down-> PartyDetails
    @enduml
    

    【讨论】:

    • 很好的答案!!!!!!谢谢@profesor79。这是一个完美的答案。我仍然不明白如何查询在 Order 集合中插入对象。你能给我一个关于订单集合的插入查询的例子吗?
    • @Vishal 添加了一些控制台代码以插入到订单集合中
    • 你能回答这个简单的问题吗:你用什么软件来生成上面发布的地图?
    • 感谢您的帮助。你是个聪明的程序员。
    • 我现在对 MongoDB 有了很好的实践。现在我已经开始在 Node.js 中创建一个应用程序。你能回答我最后一个问题吗?我的模型在 Node.js 中的订单应该是什么样子??
    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 2018-02-12
    • 2015-04-18
    • 1970-01-01
    • 2016-02-25
    相关资源
    最近更新 更多