MongoDb: fields stored in the db can't start with '$' #1296

Closed
tiagoboldt opened this Issue Jul 19, 2012 · 4 comments

Comments

Projects
None yet
4 participants

Hi,

I can use lift-mongo perfectly inside my source code, still, trying to use it inside a scalatest test I get:

fields stored in the db can't start with '$' (Bad Key: '$outer')
java.lang.IllegalArgumentException: fields stored in the db can't start with '$' (Bad Key: '$outer')

at com.mongodb.DBCollection.validateKey(DBCollection.java:1250)
at com.mongodb.DBCollection._checkKeys(DBCollection.java:1216)
at com.mongodb.DBCollection._checkObject(DBCollection.java:1206)
at com.mongodb.DBCollection.save(DBCollection.java:753)
at com.mongodb.DBCollection.save(DBCollection.java:738)
at net.liftweb.mongodb.MongoDocumentMeta$class.save(MongoDocument.scala:186)
at tests.TestDB$$anonfun$1$Person$2$.save(TestDB.scala:25)

The code I'm using to reproduce the problem is:

test("mongo connection"){


    MongoDB.defineDb(DefaultMongoIdentifier, new Mongo, "test")

    case class Address(street: String, city: String)
    case class Child(name: String, age: Int, birthdate: Option[Date])

    object Person extends MongoDocumentMeta[Person] {
      override def collectionName = "mypersons"
      override def formats = super.formats + new ObjectIdSerializer
    }

    case class Person(_id: ObjectId, name: String, age: Int, address: Address, children: List[Child], dob: Date)
      extends MongoDocument[Person] {

      def meta = Person
    }

    val p = Person(
      ObjectId.get(),
      "jofrewerge",
      27,
      Address("cenas", "coisas"),
      List(Child("joao", 20, None)),
      new Date()
    )

    p.save

}

Again, note that this same code works perfectly inside my non test code

Contributor

Dridus commented Jul 19, 2012

Move your record objects outside of the test block, e.g.

object TestRecords {
case class Address...
...
}

test("mongo connection") {
import TestRecords._

...

}

$outer is a synthetic field inserted by the compiler to support inner classes such as your record class inside of a function closure.

-Ross

On Jul 19, 2012, at 8:25 , Tiago Boldt Sousa wrote:

Hi,

I can use lift-mongo perfectly inside my source code, still, trying to use it inside a scalatest test I get:

fields stored in the db can't start with '$' (Bad Key: '$outer')
java.lang.IllegalArgumentException: fields stored in the db can't start with '$' (Bad Key: '$outer')

at com.mongodb.DBCollection.validateKey(DBCollection.java:1250)
at com.mongodb.DBCollection._checkKeys(DBCollection.java:1216)
at com.mongodb.DBCollection._checkObject(DBCollection.java:1206)
at com.mongodb.DBCollection.save(DBCollection.java:753)
at com.mongodb.DBCollection.save(DBCollection.java:738)
at net.liftweb.mongodb.MongoDocumentMeta$class.save(MongoDocument.scala:186)
at tests.TestDB$$anonfun$1$Person$2$.save(TestDB.scala:25)

The code I'm using to reproduce the problem is:

test("mongo connection"){

   MongoDB.defineDb(DefaultMongoIdentifier, new Mongo, "test")

   case class Address(street: String, city: String)
   case class Child(name: String, age: Int, birthdate: Option[Date])

   object Person extends MongoDocumentMeta[Person] {
     override def collectionName = "mypersons"
     override def formats = super.formats + new ObjectIdSerializer
   }

   case class Person(_id: ObjectId, name: String, age: Int, address: Address, children: List[Child], dob: Date)
     extends MongoDocument[Person] {

     def meta = Person
   }

   val p = Person(
     ObjectId.get(),
     "jofrewerge",
     27,
     Address("cenas", "coisas"),
     List(Child("joao", 20, None)),
     new Date()
   )

   p.save

}

Again, note that this same code works perfectly inside my non test code


Reply to this email directly or view it on GitHub:
#1296

Owner

Shadowfiend commented Jul 19, 2012

Please don't post issues without discussing them on the Lift list first. See http://liftweb.net/community for more info.

thanks for the quick reply.
@Shadowfiend makes sense, but it will be searchable now, mailing list contents are not as well indexed. But I'll make sure to it next time :)

Contributor

nafg commented Jul 20, 2012

Sounds like the models have to be top level classes.

On Thursday, July 19, 2012, Tiago Boldt Sousa wrote:

Hi,

I can use lift-mongo perfectly inside my source code, still, trying to use
it inside a scalatest test I get:

fields stored in the db can't start with '$' (Bad Key: '$outer')
java.lang.IllegalArgumentException: fields stored in the db can't start
with '$' (Bad Key: '$outer')

    at com.mongodb.DBCollection.validateKey(DBCollection.java:1250)
    at com.mongodb.DBCollection._checkKeys(DBCollection.java:1216)
    at com.mongodb.DBCollection._checkObject(DBCollection.java:1206)
    at com.mongodb.DBCollection.save(DBCollection.java:753)
    at com.mongodb.DBCollection.save(DBCollection.java:738)
    at

net.liftweb.mongodb.MongoDocumentMeta$class.save(MongoDocument.scala:186)
at tests.TestDB$$anonfun$1$Person$2$.save(TestDB.scala:25)

The code I'm using to reproduce the problem is:

test("mongo connection"){


    MongoDB.defineDb(DefaultMongoIdentifier, new Mongo, "test")

    case class Address(street: String, city: String)
    case class Child(name: String, age: Int, birthdate: Option[Date])

    object Person extends MongoDocumentMeta[Person] {
      override def collectionName = "mypersons"
      override def formats = super.formats + new ObjectIdSerializer
    }

    case class Person(_id: ObjectId, name: String, age: Int, address:

Address, children: List[Child], dob: Date)
extends MongoDocument[Person] {

      def meta = Person
    }

    val p = Person(
      ObjectId.get(),
      "jofrewerge",
      27,
      Address("cenas", "coisas"),
      List(Child("joao", 20, None)),
      new Date()
    )

    p.save

}

Again, note that this same code works perfectly inside my non test code


Reply to this email directly or view it on GitHub:
#1296

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment