Malcolm O'Hare
Malcolm O'Hare

Reputation: 4999

MongoDB Find based on contents of a collection in the record

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

Answers (1)

Philipp
Philipp

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

Related Questions