Pal Tech Academy
Pal Tech Academy

Reputation: 311

Is it possible to create temporary collection in mongoDB like temp table in sql server?

I Want to create a temp collection in mongodb just like temp table in Sql Server.If it is possible then please let me know how can I do this.

Upvotes: 12

Views: 21159

Answers (3)

ray
ray

Reputation: 15275

In my opinion, the closest thing you can use as an alternative to SQL server temp table is views.

For the common use case of temporary data processing/aggregation, you can use the standard view. The standard view is created by using aggregation pipeline, in which you process/aggregate data from single or multiple collections. You can then use the resulting view just like the way you can use in a standard collection / the way of using temp table in SQL server. The view is computed in an ad-hoc way when you read the view.

If ad-hoc computation is considered costly for your use case, you can consider using on-demand materialized view. This is also created through an aggregation pipeline, but with using $out as the last aggregation stage. The difference between on-demand materialized view and the aforementioned standard view is that on-demand materialized view is stored on the disk, which serves as a normal collection for you to query on and building indexes on it.

Read more on this official doc

Upvotes: 0

shivangi dhakad
shivangi dhakad

Reputation: 379

You can use $out operator introduced in version 2.6, where you can write the output of your aggregation pipeline into a collection and use it as a temporary collection, which can be deleted after it served your purpose.

Upvotes: 8

sergiuz
sergiuz

Reputation: 5529

No there are no temporary collections.

You may have a look at capped collections which "are fixed-size collections" and they behave like this: "once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection."

Upvotes: 3

Related Questions