У меня есть два пользователя и продукта calss модели, и у меня есть общий продукт propertyId. Я хочу получить данные пользователя от Mongodb. Я хочу, чтобы пользователь подключался к продукту под ним, который должен отображаться.Ссылка на данные в mongodb
модель пользователя
класса пользователь общественности: Продукт {
[BsonId]
public string GuId { get; set; }
public string Name { get; set; }
public string EMailAddress { get; set; }
public string ProductId { get; set; }
}
модель продукта
общественный класс продукт {
[BsonId]
public int Id { get; set; }
public string ProductId { get; set; }
public string ProductName { get; set; }
}
И мое приложение слой имеет следующий код: -
public IEnumerable<User> GetAllUsers()
{
// var users = new User();
IList<User> itemCollection = new List<User>();
var itemIdList = new List<string>();
itemCollection = _users.Include<User>.Load(itemIdList.ToArray())
.OrderByDescending(x => x.LastUpdatedDate).ToList();
// var itemCollection = _users.Load<User>(itemIdList.ToArray());
this.LoadItemStatusDefinitions(itemCollection);
var users = _users.
FindAll().
SetSortOrder(SortBy.Descending("createdate"));
return users;
}
private void LoadItemStatusDefinitions(IEnumerable<User> items)
{
// iterates through each items in the collection.
foreach (var item in items)
{
// check whether status definition is already assigned to the item
if (!string.IsNullOrEmpty(item.ProductId))
{
// loading the status definition associated with each item
var statusDefinition = _users.Load<Product>(item.ProductId);
if (statusDefinition != null)
{
// assign the status definition to the item.
item.ProductId = statusDefinition;
}
}
}
}
Если вы хотите сделать это в одном извлечения, вы не сможете. Вам нужно будет сделать два вызова findOne. Или вставьте все данные в один документ. – WiredPrairie