在Mongodb C# drivers 文档
官方地址:https://docs.mongodb.com/ecosystem/drivers/csharp/
基础的使用请参考《c# Mongodb》
原本在Nodejs中是可以直接某个字段参考自另一个表的,数据类型是ObjectID,只需要加入ref:'某个实体'即可实现
'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类型的处理方式,
找到部分资料,但还没有悟透方法
参考地址:https://oz-code.com/blog/how-to-mongodb-in-c-part-1/
使用Linq语法参考https://jira.mongodb.org/browse/CSHARP-1627
在没有找到相关的方法时,先使用以下代码代替,即使用Linq的语法进行处理
/// <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; } }