OP_MSG create time series collection

Hello, this is my situation:
I am inserting data like this: (timestamp not in correct format here, im using byte arrays)

  "insert": "collection-name", 
  "documents": [
      "timestamp": 1707897135000,
      "key": "value"
  "$db": "db-name"

This would create a regular collection of name collection-name
but I’d like to create a time series collection, this can be created using a separate query, but how is this done using OP_MSG? I cant seem to find any info on it, any links to documentation or general info would be useful, most useful so far has been
mongodb specifications OP_MSG
but that only shows insertion/deletion/updates regularly


Hey @warben1,

OP_MSG is a MongoDB Wire Protocol Opcode and is the mechanism by which drivers package database commands and send them over a socket connection to a server component (mongos or mongod).

Per the example above, you’re sending an insert command, which contains an array of one or more documents. The insert command doesn’t have any mechanism to differentiate whether the target collection (collection-name in this case) has been created with any specific options (see create command docs), so implicit collection creation would just be handled on the server.

If you’re going to be working with time series data it makes sense (and is required) to create the collection explicitely before writing to it.

If your question is “how do I create the time series collection using a driver”, you could do this by issuing the create command directly. For example, the Node.js driver allows you to run a command as follows:

const commandDoc = {
  create: "collection-name",
  timeseries: {
    timeField: "timestamp",
    metaField: "metadata"
const result = await myDB.command(commandDoc);
1 Like

Is it possible to create collection explicitly through op_msg?
like for example sending
{ "create": "collection-name", "timeseries": {"timeField": "timestamp", "metaField": "metadata"} }
for this query im getting
{ “ok” : 0.0, “errmsg” : “OP_MSG requests require a $db argument”, “code” : 40571, “codeName” : “Location40571” } as return
(I’m using tcp/ip and simply sending via a socket)

nvm i just forgot $db at end
{ "create": "collection-name", "timeseries": {"timeField": "timestamp", "metaField": "metadata"} "$db": "new-ts-test"}
{ “ok” : 1.0 }

Thanks a lot!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.