在Mongodb C# drivers 文檔php
官方地址:https://docs.mongodb.com/ecosystem/drivers/csharp/html
基礎的使用請參考《c# Mongodb》mongodb
本來在Nodejs中是能夠直接某個字段參考自另外一個表的,數據類型是ObjectID,只須要加入ref:'某個實體'便可實現c#
'use strict'; import mongoose from './db'; let Schema = mongoose.Schema; let jobOrderSchema = new Schema({ JobOrderCode: { type: String, unique: true, trim: true, required: [true, 'Kindly enter the jobOrder code'] }, JobOrderName: { type: String, trim: true, required: [true, 'Kindly enter the jobOrder name'] }, JobOrderDesc: { type: String, trim: true, required: [true, 'Kindly enter the jobOrder description'] }, OrderCount: { type: Number, required: [true, 'Kindly enter the order count'] }, CustomerID: { type: Schema.Types.ObjectId, ref: 'Customer' }, MaterialID: { type: Schema.Types.ObjectId, ref: 'Material' }, MouldID: { type: Schema.Types.ObjectId, ref: 'Mould' }, MachineID:{ type: Schema.Types.ObjectId, ref: 'Machine' }, MachineAcceptLog:[{ type: String, trim: true }], Remark: { type: String, trim: true }, CreateAt: { type: Date, default: Date.now }, CreateBy: { type: String }, LastUpdateAt: { type: Date, default: Date.now }, LastUpdateBy: { type: String }, }); jobOrderSchema.set('collection', 'JobOrder'); const jobOrderModel = mongoose.model('JobOrder', jobOrderSchema); export default jobOrderModel;
但在C#中,暫時尚未找到ref類型的處理方式,mongoose
找到部分資料,但尚未悟透方法 ui
參考地址:https://oz-code.com/blog/how-to-mongodb-in-c-part-1/ code
使用Linq語法參考https://jira.mongodb.org/browse/CSHARP-1627 htm
在沒有找到相關的方法時,先使用如下代碼代替,即便用Linq的語法進行處理blog
/// <summary> /// 獲取全部未開始的訂單 /// </summary> /// <returns></returns> public static object GetJobOrderByNoStart() { try { var customerCollection = Common.MongodbHandler.GetInstance().mc_MongoDatabase.GetCollection<DataModel.Customer>(Common.ConfigFileHandler.GetAppConfig("CustomerCollectionName")); var materialCollection = Common.MongodbHandler.GetInstance().mc_MongoDatabase.GetCollection<DataModel.Material>(Common.ConfigFileHandler.GetAppConfig("MaterialCollectionName")); var mouldCollection = Common.MongodbHandler.GetInstance().mc_MongoDatabase.GetCollection<DataModel.Mould>(Common.ConfigFileHandler.GetAppConfig("MouldCollectionName")); var jobOrderCollection = Common.MongodbHandler.GetInstance().mc_MongoDatabase.GetCollection<DataModel.JobOrder>(Common.ConfigFileHandler.GetAppConfig("JobOrderCollectionName")); var getdocument = (from jo in jobOrderCollection.AsQueryable() join cu in customerCollection.AsQueryable() on jo.CustomerID equals cu._id join ma in materialCollection.AsQueryable() on jo.MaterialID equals ma._id join mo in mouldCollection.AsQueryable() on jo.MouldID equals mo._id where jo.Status == null select new { JobOrderCode = jo.JobOrderCode, JobOrderName = jo.JobOrderName, OrderCount = jo.OrderCount, JobOrderDesc = jo.JobOrderDesc, Status = ( jo.Status == null ? "未開始" : jo.Status == "Process" ? "生產中" : jo.Status == "Stop" ? "暫停" : "未知狀態" ), CustomerCode = cu.CustomerCode, CustomerName = cu.CustomerName, MaterialCode = ma.MaterialCode, MaterialName = ma.MaterialName, MaterialSpecification = ma.MaterialSpecification, MouldCode = mo.MouldCode, MouldName = mo.MouldName, MouldSpecification = mo.MouldSpecification, } ).ToList(); return getdocument; } catch (Exception ex) { throw ex; } }