代码之家  ›  专栏  ›  技术社区  ›  seinecle

如何使用Morphia将对象集合保存到MongoDB?

  •  1
  • seinecle  · 技术社区  · 12 年前

    我目前正在迭代一组对象,并逐个保存它们。我想有一种方法可以直接保存整个收藏?

    请注意,我稍后需要能够检索每个单独的对象(而不是整个集合)。

    1 回复  |  直到 12 年前
        1
  •  2
  •   gkamal    12 年前

    来自DataStore类的javadoc。在旧版本上可能不可用。即使这样,我也认为是在内部迭代并一次保存一个,但它可能会得到优化,所以使用它比自己迭代要好。

    <T> Iterable<Key<T>>
    save(Iterable<T> entities) 
              Saves the entities (Objects) and updates the @Id field
    <T> Iterable<Key<T>>
    save(Iterable<T> entities, com.mongodb.WriteConcern wc) 
              Saves the entities (Objects) and updates the @Id field, with the WriteConcern
    <T> Iterable<Key<T>>
    save(T... entities) 
              Saves the entities (Objects) and updates the @Id field
    

    http://morphia.googlecode.com/svn/site/morphia/apidocs/com/google/code/morphia/Datastore.html#save(java.lang.Iterable )