xlaok
xlaok

Reputation: 597

how to query on all hash values in mongoid?

I have a hash filed in mongodb like :

Record document   
cells: {
            "5742a6af744f6b0dcf0003d1"=>"1",
            "5742a6af744f6b0dcf0003d2"=>"12"
            "5742a6af744f6b0dcf0003d3"=>"12"
        }

how could I query on cells where cells.values equal 12 ?

Upvotes: 0

Views: 239

Answers (1)

Emu
Emu

Reputation: 5895

You can't do that query in MongoDB. You need to format the collection data like the following:

{
  "_id": ObjectId("5406e4c49b324869198b456a"),
  "cells": [
    { "number": "5742a6af744f6b0dcf0003d1", "value": 1 },
    { "number": "5742a6af744f6b0dcf0003d2", "value": 12 },
    { "number": "5742a6af744f6b0dcf0003d3", "value": 12 }
  }
}

Then you can query like:

db.collection_name.find({'cells.value': { $eq: 12 }})

Your data format is actually "anti-pattern". That's why it is not possible.

Hope it helps!

Upvotes: 1

Related Questions