0

In a NoSQL database (MongoDB), I need to store a list of creditcards for a user and to keep track of which one is active.

I came across 3 patterns:

#1 Keep the active property in the Creditcard schema

User
| _id: String
| creditcards: [Creditcard]
| ...

Creditcard
| _id: String
| active: Boolean
| ...

#2 Keep the reference of the active Creditcard in the User's schema

User
| _id: String
| creditcards: [Creditcard]
| activeCreditcard: Creditcard
| ...

Creditcard
| _id: String
| ...

#3 Store a list of creditcard and active objects in the User's schema

User
| _id: String
| creditcards: [{ creditcard: Creditcard, active: Boolean }]
| ...

Creditcard
| _id: String
| ...

I started to implement the first one but when I needed to update the creditcards and which one was active, I wasn't sure that this was the best pattern.

Hence, I tried to find different solutions but I can't choose. Any recommendations?

1

The first one looks too relational to me for MongoDB. I know that's not a sin, but it looks like it is for an RDBMS. For a document database I would keep it all as one document and simply mark one as active. It is like #2, but I don't know what that second CreditCard document does.

-------------------
 key               
 ------------------
 _id               
 customer_id       
 credit_card       
 active  



db.customer_card.insert({customer_id: "AFB123", credit_card: 1234567812347890, active:true} )

db.customer_card.find({customer_id: {$eq: "AFB123"}, active:{$eq: true})
or
db.customer_card.find({customer_id: "AFB123", active:true})

You will have to test the commands. I could not test them.

  • The Creditcard schema is shared with another collection so I need to keep it as a separate schema. – Kerumen Dec 29 '16 at 16:35
  • I didn't think MongoDB had a schema, per se. – johnny Dec 29 '16 at 16:39
  • I don't understand, what you described is like #1? – Kerumen Dec 29 '16 at 16:39
  • No. I put it all in one. – johnny Dec 29 '16 at 16:40
  • @Kerumen I'm saying I'd duplicate data. – johnny Dec 29 '16 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.