////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

=== GORM for MongoDB without Grails
If you wish to use GORM for MongoDB outside of a Grails application you should declare the necessary dependencies, for example in Gradle:

[source,groovy,subs="attributes"]
----
compile "org.grails:grails-datastore-gorm-mongodb:{version}"
----

Then annotate your entities with the `grails.gorm.annotation.Entity` annotation:

[source,groovy]
----
@Entity
class Person {
    String name
}
----

Then you need to place the bootstrap logic somewhere in the loading sequence of your application which uses `MongoDatastore`:

[source,groovy]
----
def datastore = new MongoDatastore(Person)

println Person.count()
----

You can also supply your own `com.mongodb.client.MongoClient` if you need to configure the driver directly:

[source,groovy]
----
MongoClient mongoClient = MongoClients.create("mongodb://localhost/mydb")
def datastore = new MongoDatastore(mongoClient, Person)

println Person.count()
----

NOTE: When you supply your own `MongoClient`, GORM does not close it when the datastore shuts down — the client is owned by whoever created it, so you are responsible for closing it. When GORM creates the client itself (for example `new MongoDatastore(Person)`), it closes it for you.

For configuration you can either pass a map or an instance of the `org.springframework.core.env.PropertyResolver` interface:

[source,groovy]
----
def initializer = new MongoDatastore(['grails.mongodb.url':'http://myserver'], Person)

println Person.count()
----

If you are using Spring with an existing `ApplicationContext` you can instead call `MongoDbDataStoreSpringInitializer.configureForBeanDefinitionRegistry` prior to refreshing the context. You can pass the Spring `Environment` object to the constructor for configuration:

[source,groovy]
----
ApplicationContext myApplicationContext = ...
def initializer = new MongoDbDataStoreSpringInitializer(myApplicationContext.getEnvironment(), Person)
initializer.configureForBeanDefinitionRegistry(myApplicationContext)

println Person.count()
----
