F
F
FaionWeb2015-12-08 09:29:45
ruby
FaionWeb, 2015-12-08 09:29:45

How to set a composite index in MongoDB with Ruby Driver?

From the documentation you can see


require 'mongo'
include Mongo
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'publicdb')
client[:con].indexes.create_many([
{ :key => { name: 1 }},
{ :key => { label: -1 }}
])

The index is created fine, but I need to index the field from the parent, in MongoShell
db.con.createIndex({"name.tree":1})
, when trying to tell Ruby the string {:key => {name.tree: 1}} gives an error
syntax error, unexpected ':', expecting =>
{ :key => { name.wer: 1 }},

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FaionWeb, 2015-12-08
@FaionWeb

Solution - you need to read the documentation carefully)

index = "name.tree"
client[:con].indexes.create_one({ index => 1 })

, but what I tried to do at the beginning was building multiple indexes
The question is closed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question