1

I have structure class like this:

#<Transaction _id: 54d46d6a6e6f626fbcc70000,  _keywords: ["1", "test1", "test2", "abc1", "projectmongo", "last2", "taka"]>

array keyword map with: [field1, field2,...] So I want to query to get all transaction have field1 == "1" Is this possible?

2
  • 2
    Do you mean fetching all Transaction records, where only first element in _keywords array == "1"? Commented Mar 3, 2015 at 9:23
  • fetching All, and merge with other filters. Like it: Transaction.where(:company=> params[:company]).where(:_keywords.0 => "1") Commented Mar 3, 2015 at 10:33

1 Answer 1

2

MongoDB "dot notation". Don't expect there to be a "rails like" SQL ORM mapping equivalent when using functionality like arrays which are generally not supported for those storage engines:

Class.collection.find({ "_keywords.0" => "1" })

So the Moped syntax here is more raw to the MongoDB functionality.

That basically says "look at the first array element to see if it matches the value I ask for".

Sign up to request clarification or add additional context in comments.

3 Comments

I use "where" to query. Beacause it have many ohter filters. Can you write it in rails mongoid syntax, please ?
@Peter_175 The very point I made is that there "is no Rails Syntax" for this because there is nothing that correlates to SQL Datastores in what you are asking. It works. If you expect it to work across different storage engines then you have a how different problem to solve.
Yep. So I must convert "where" syntax to collection.find. Thanks @Neil :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.