Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store multiple types of Objects into 1 mongodb collection?

using document oriënted database mongodb and the Object Document Mapper (ODM) morphia

Lets say we have 3 different classes; Object, Category and Action.
These object are all stored in the collections; objects, categories and actions.

Category and Action are references of Object

@Entity("objects")
public class Object {

    @Id
    @Property("id")
    private ObjectId id;

    @Reference
    private Category category;
    private Action action;
    ...
}

@Entity("categories")
public class Category {

    @Id
    public String categoryTLA;
    public String categoryName;
    ...
}

@Entity("actions")
public class Action implements BaseEntity {

    @Id
    public String action;
    public int accesLevel;
    ...
}

The documents with the current implementation are stored like:

  • Mongo (Server/location)
    • store (database)
      • objects (collection)
        • object (document)
        • object
        • object
      • categories
        • categorie
        • categorie
        • categorie
      • actions
        • action
        • action
        • action

Is it possible to store 2 different Objects, in this case Category and Action, in one collection, like shown in the next example? Both with their own identification!

  • Mongo
    • store
      • objects
        • object
        • object
        • object
      • settings
        • categorie
        • categorie
        • categorie
        • action
        • action
        • action
like image 569
Marcel Avatar asked Mar 05 '14 15:03

Marcel


People also ask

Can we have multiple collections in MongoDB?

Yes, you can have multiple collections within a database in MongoDB.

How many documents can be stored in MongoDB collection?

MongoDB supports no more than 100 levels of nesting for BSON documents. Each object or array adds a level.

Which of these types of data can be stored in a MongoDB collection?

MongoDB uses Binary JSON and MQL as an alternative to SQL. BSON allows for data types such as the floating-point, long, date, and many more that are not supported by regular JSON.

Can you store objects in MongoDB?

Large objects, or "files", are easily stored in MongoDB. It is no problem to store 100MB videos in the database.


1 Answers

yes. but probably you need to add field "documentType" to any document to distinguish documents

like image 182
vodolaz095 Avatar answered Oct 13 '22 00:10

vodolaz095