Reputation: 4999
I have a collection of User being stored in MongoDB
Given an invoice number, how would I query the User collection to get the associated user? (Find user in collection where Payments contains record with InvoiceNumber = '###')
public class User
{
[BsonId]
public ObjectId ID { get; set; }
public string UserName {get;set;}
public List<PaymentInfo> Payments {get;set;}
}
public class PaymentInfo
{
public string CardNumber {get;set;}
public int InvoiceNumber {get;set;}
public float Amount {get;set;}
}
Upvotes: 0
Views: 84
Reputation: 69663
Arrays are transparent to MongoDB. So
db.yourCollection.find({Payments.InvoiceNumber: 1234})
should return all documents where the payments array contains one or more objects where InvoiceNumner = 1234.
Upvotes: 1