1.为集合新增1个classl类ShippingAddress
思路:从集合中按条读取记录,赋值给新的类,然后写回去。
正确代码
#region Utilities
protected virtual void UpgradeProductStockForShops()
{
//come from:https://www.iaspnetcore.com/blog/blogpost/5f5d74e59122f001e499d701/mongodb-c-set-add-a-new-field-find-delete-the-record-of-a-value-and-change-the-field-type
var lproductStockForShops = from p in this._productStockForShopRepository.Table
select p;
//search engine names
foreach (var productStockForShop in lproductStockForShops)
{
//集合中有数据时不动
if (productStockForShop.ShippingAddress == null)
productStockForShop.ShippingAddress = new Address();
_productStockForShopRepository.Update(productStockForShop);
}
}
#endregion
C# 增、减mongodb子文档ChildDocument Document
https://www.cnblogs.com/gaobing/p/5667021.html
2.原子操作:更新点击数
/// <summary>
/// GetViewCount 已优化,改为原子操作
/// </summary>
/// <param name="blogPostId"></param>
/// <returns></returns>
public int UpdateViewCount(string blogPostId)
{
//更新点击数
Task.Run(() =>
{
var update = new UpdateDefinitionBuilder<BlogPost>().Inc(x => x.ViewCount, 1);
var result = _blogPostRepository.Collection.UpdateManyAsync(x => x.Id == new ObjectId(blogPostId), update).Result;
_cacheManager.RemoveByPattern(string.Format(BLOGPOST_BY_ID_KEY, blogPostId));
});
//获取点击数
//filter
var filter = Builders<BlogPost>.Filter.Eq(x => x.Id, new ObjectId(blogPostId));
//projection
var projection = Builders<BlogPost>.Projection.Include(x => x.ViewCount);
var results = _blogPostRepository.Collection.Find(filter)
.Project<BlogPost>(projection)
.FirstOrDefault();
return results.ViewCount;
}
正确结果:
{
"_id" : ObjectId("5f5c388909b35204ce080f12"),
"Appid" : "wx9b00e80b6177fc26",
"AuthorOpenid" : "oTC2f4qAPrHc7jSIzuG1GiVvri20",
"AuthorUnionid" : "on1X2weBgRYKD3zeIdFN47258RuM",
"OriginalId" : "gh_96b119488287",
"Sku" : null,
"Name" : null,
"OrderNumber" : "20200912001",
"ProducTypeId" : null,
"ProducType" : {
"_id" : null,
"Name" : null,
"IsDeleted" : false,
"CreatedOnUtc" : ISODate("0001-01-01T08:05:43.000+08:00"),
"UpdatedOnUtc" : ISODate("0001-01-01T08:05:43.000+08:00")
},
"Quantity" : 1,
"Price" : "1200",
"ProductStockOutDate" : ISODate("2020-09-12T20:00:01.000+08:00"),
"Mark" : "带安",
"WarehouseId" : null,
"Warehouse" : {
"_id" : null,
"Name" : null,
"IsDeleted" : false,
"CreatedOnUtc" : ISODate("0001-01-01T08:05:43.000+08:00"),
"UpdatedOnUtc" : ISODate("0001-01-01T08:05:43.000+08:00")
},
"ShippingAddress" : {
"_id" : null,
"Appid" : null,
"AuthorOpenid" : null,
"AuthorUnionid" : null,
"OriginalId" : null,
"CustomerId" : null,
"FirstName" : null,
"LastName" : null,
"Email" : null,
"Company" : null,
"VatNumber" : null,
"CountryId" : null,
"StateProvinceId" : null,
"City" : null,
"Address1" : null,
"Address2" : null,
"ZipPostalCode" : null,
"PhoneNumber" : null,
"FaxNumber" : null,
"CustomAttributes" : null,
"CreatedOnUtc" : ISODate("0001-01-01T08:05:43.000+08:00"),
"UserName_zh_CN" : null,
"Mobile1" : null,
"Mobile2" : null,
"Mobile" : [ ],
"InstalledOnUtc" : ISODate("0001-01-01T08:05:43.000+08:00")
},
"IsDeleted" : false,
"LastloginIP" : null,
"CreatedOnUtc" : ISODate("2020-09-12T11:06:06.706+08:00"),
"UpdatedOnUtc" : ISODate("2020-09-12T11:06:07.735+08:00")
},
过程:
public partial class Address : BaseEntity
{
public string Appid { get; set; }
public string AuthorOpenid { get; set; }
public string AuthorUnionid { get; set; }
public string OriginalId { get; set; }
public string CustomerId { get; set; }
/// <summary>
/// Gets or sets the first name
/// </summary>
public string FirstName { get; set; }
/// <summary>
/// Gets or sets the last name
/// </summary>
public string LastName { get; set; }
/// <summary>
/// Gets or sets the email
/// </summary>
public string Email { get; set; }
/// <summary>
/// Gets or sets the company
/// </summary>
public string Company { get; set; }
/// <summary>
/// Gets or sets the vat numer
/// </summary>
public string VatNumber { get; set; }
/// <summary>
/// Gets or sets the country identifier
/// </summary>
public string CountryId { get; set; }
/// <summary>
/// Gets or sets the state/province identifier
/// </summary>
public string StateProvinceId { get; set; }
/// <summary>
/// Gets or sets the city
/// </summary>
public string City { get; set; }
/// <summary>
/// Gets or sets the address 1
/// </summary>
public string Address1 { get; set; }
/// <summary>
/// Gets or sets the address 2
/// </summary>
public string Address2 { get; set; }
/// <summary>
/// Gets or sets the zip/postal code
/// </summary>
public string ZipPostalCode { get; set; }
/// <summary>
/// Gets or sets the phone number
/// </summary>
public string PhoneNumber { get; set; }
/// <summary>
/// Gets or sets the fax number
/// </summary>
public string FaxNumber { get; set; }
/// <summary>
/// Gets or sets the custom attributes (see "AddressAttribute" entity for more info)
/// </summary>
public string CustomAttributes { get; set; }
/// <summary>
/// Gets or sets the date and time of instance creation
/// </summary>
public DateTime CreatedOnUtc { get; set; }
#region custom extend
public string UserName_zh_CN { get; set; }
public string Mobile1 { get; set; }
public string Mobile2 { get; set; }
/// <summary>
/// Gets or sets the phone number
/// </summary>
public List<string> Mobile { get; set; } = new List<string>();
/// <summary>
/// 设备安装日期
/// </summary>
public DateTime InstalledOnUtc { get; set; }
#endregion
}
step 1:
#region Utilities
protected virtual void UpgradeProductStockForShops()
{
var lproductStockForShops = from p in this._productStockForShopRepository.Table
select p;
//search engine names
foreach (var productStockForShop in lproductStockForShops)
{
//重新写入集合
_productStockForShopRepository.Update(productStockForShop);
}
}
结果:
"ShippingAddress" : null,
{
"_id" : ObjectId("5f5c388909b35204ce080f12"),
"Appid" : "",
"AuthorOpenid" : "",
"AuthorUnionid" : "",
"OriginalId" : "",
"Sku" : null,
"Name" : null,
"OrderNumber" : "20200912001",
"ProducTypeId" : null,
"ProducType" : {
"_id" : null,
"Name" : null,
"IsDeleted" : false,
"CreatedOnUtc" : ISODate("0001-01-01T08:05:43.000+08:00"),
"UpdatedOnUtc" : ISODate("0001-01-01T08:05:43.000+08:00")
},
"Quantity" : 1,
"Price" : "1200",
"ProductStockOutDate" : ISODate("2020-09-12T20:00:01.000+08:00"),
"Mark" : "安",
"WarehouseId" : null,
"Warehouse" : {
"_id" : null,
"Name" : null,
"IsDeleted" : false,
"CreatedOnUtc" : ISODate("0001-01-01T08:05:43.000+08:00"),
"UpdatedOnUtc" : ISODate("0001-01-01T08:05:43.000+08:00")
},
"ShippingAddress" : null,
"IsDeleted" : false,
"LastloginIP" : null,
"CreatedOnUtc" : ISODate("2020-09-12T11:06:06.706+08:00"),
"UpdatedOnUtc" : ISODate("2020-09-12T11:06:07.735+08:00")
},
直接生成新的类,发现结果是正确的。
http://localhost:5000/api/ProductStockForShop/NewProductStockForShop
{"Appid":null,"AuthorOpenid":null,"AuthorUnionid":null,"OriginalId":null,"Sku":null,"Name":null,"OrderNumber":null,"ProducTypeId":null,"ProducType":{"Name":null,"IsDeleted":false,"CreatedOnUtc":"0001-01-01T00:00:00","UpdatedOnUtc":"0001-01-01T00:00:00","_id":null},"Quantity":1,"Price":0,"ProductStockOutDate":"2020-09-13T00:56:19.3432457Z","Mark":null,"WarehouseId":null,"Warehouse":{"Name":null,"IsDeleted":false,"CreatedOnUtc":"0001-01-01T00:00:00","UpdatedOnUtc":"0001-01-01T00:00:00","_id":null},"ShippingAddress":{"Appid":null,"AuthorOpenid":null,"AuthorUnionid":null,"OriginalId":null,"CustomerId":null,"FirstName":null,"LastName":null,"Email":null,"Company":null,"VatNumber":null,"CountryId":null,"StateProvinceId":null,"City":null,"Address1":null,"Address2":null,"ZipPostalCode":null,"PhoneNumber":null,"FaxNumber":null,"CustomAttributes":null,"CreatedOnUtc":"0001-01-01T00:00:00","UserName_zh_CN":null,"Mobile1":null,"Mobile2":null,"Mobile":[],"InstalledOnUtc":"0001-01-01T00:00:00","_id":null},"IsDeleted":false,"LastloginIP":null,"CreatedOnUtc":"0001-01-01T00:00:00","UpdatedOnUtc":"0001-01-01T00:00:00","_id":null}
原因分析:
因为集合中没有ShippingAddress,类中的ShippingAddress被设置为null了。