Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when inserting into time series collections #903

Open
pavlos163 opened this issue Dec 10, 2024 · 1 comment
Open

Fix error when inserting into time series collections #903

pavlos163 opened this issue Dec 10, 2024 · 1 comment

Comments

@pavlos163
Copy link

pavlos163 commented Dec 10, 2024

Versions

  • NodeJS: 20.11.1
  • mongodb-memory-server-*: 10.1.2
  • mongodb(the binary version): 8.0.3
  • mongoose: 8.8.2
  • system: Linux Debian (from node:20.11.1-slim Docker image)

package: mongo-memory-server

What is the Problem?

When using mongodb-memory-server with a timeseries collection in test environment, attempting to insert documents results in MongoServerError: Namespace X is a view, not a collection. The same code works correctly with a local MongoDB instance.

This appears to be a discrepancy in how mongodb-memory-server handles timeseries collections compared to a standard MongoDB installation.

Code Example

// Model definition
const postSchema: Schema = new mongoose.Schema(
  {
    date: {
      type: Date,
      required: true
    },
    content: {
      type: String,
      required: true
    }
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
    timeseries: {
      timeField: "date",
      granularity: "hours"
    },
    autoCreate: true,
    timestamps: true
  }
);

export const Post = mongoose.model<PostDocument>("Post", postSchema);

// Test setup
const { MongoMemoryReplSet } = require("mongodb-memory-server");

const MONGO_VERSION = "8.0.3";

module.exports = async () => {
  const replicaSet = await MongoMemoryReplSet.create({
    instanceOpts: [
      {
        port: parseInt(process.env.DATABASE_IN_MEMORY_PORT)
      }
    ],
    binary: {
      version: MONGO_VERSION
    },
    replSet: { count: 1, storageEngine: "wiredTiger" }
  });
};

// Test that fails
describe("createAllPosts", () => {
  beforeAll(async () => {
    await buildPost({
      content: "test content",
    });
  });

  it("should work with timeseries collection", async () => {
    const posts = await Post.find();
    expect(posts.length).toBe(1);
  });
});

// helpers
async function buildPost(
  overrides: Partial<PostDTOInterface> = {}
): Promise<PostDocument> {
  const data: PostDTOInterface = {
    date: new Date(Date.now()),
    content: faker.lorem.sentence(),
    ...overrides
  };

  return new Post(data).save();
}

Error output

MongoServerError: Namespace PostCronService.posts is a view, not a collection
    at InsertOneOperation.execute (node_modules/mongodb/src/operations/insert.ts:82:13)
    at tryOperation (node_modules/mongodb/src/operations/execute_operation.ts:271:14)
    at executeOperation (node_modules/mongodb/src/operations/execute_operation.ts:109:12)
    at Collection.insertOne (node_modules/mongodb/src/collection.ts:277:12)

Do you know why it happens?

The issue seems specific to mongodb-memory-server as:

  • The exact same code works correctly with a local MongoDB instance
  • MongoDB 8.0.3 fully supports timeseries collections
@pavlos163 pavlos163 added the bug label Dec 10, 2024
@pavlos163 pavlos163 changed the title Error when inserting into TimeSeries collections (MongoDB v8) Error when inserting into time series collections (MongoDB v8) Dec 10, 2024
@pavlos163 pavlos163 changed the title Error when inserting into time series collections (MongoDB v8) Error when inserting into time series collections Dec 10, 2024
@pavlos163 pavlos163 changed the title Error when inserting into time series collections Fix error when inserting into time series collections Dec 10, 2024
@hasezoey
Copy link
Member

hasezoey commented Dec 10, 2024

When using mongodb-memory-server with a timeseries collection in test environment, attempting to insert documents results in MongoServerError: Namespace X is a view, not a collection

That sounds like a weird error.

This appears to be a discrepancy in how mongodb-memory-server handles timeseries collections compared to a standard MongoDB installation.

mongodb-memory-server is basically a local installation, as MMS literally only handles download & running mongod binaries, so there shouldnt be a discrepancy with the same options.


In what part of the provided code does the error occur?
What version were you using when testing a "local installation"? (was that also a repl-set?)

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

No branches or pull requests

2 participants