fix collection type definition: `_id` field should be optional on inserts
- Dominant language
- TypeScript
- Stars
- 41
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Description
I basically copied the example on the docs and am getting the error in the title.
code:
```ts
import {
MongoClient,
ObjectId,
} from "https://deno.land/x/atlas_sdk@v1.0.3/mod.ts";
import { config } from "https://deno.land/x/dotenv@v3.2.0/mod.ts";
const client = new MongoClient({
endpoint: `https://data.mongodb-api.com/app/${
config().DB_APP_ID
}/endpoint/data/v1`,
dataSource: config().DB_DATA_SOURCE,
auth: {
apiKey: config().DB_API_KEY,
},
});
interface Users {
_id: ObjectId;
name: string;
email: string;
}
const db = client.database(config().DB_NAME);
const users = db.collection("users");
// error is here
users.insertOne({ // <---
name: "John Doe",
email: "johndoe@gmail.com",
});
```
error:
```
Argument of type '{ name: string; email: string; }' is not assignable to parameter of type 'Users'.
Property '_id' is missing in type '{ name: string; email: string; }' but required in type 'Users'.deno-ts(2345)
```
### Update:
This was proposed by Github Copilot and seems to work:
```ts
users.insertOne({
name: "John Doe",
email: "johndoe@gmail.com",
_id: new ObjectId(),
});
```
### Update:
Works:

Contributor guide
No contributing guide indexed for this repository
Research direction
Trace the collection generic and insertOne type definition used by the TypeScript SDK, starting from the failing call shown in the issue. Make the _id field optional for inserts while preserving generated or supplied IDs, then verify that the documented example type-checks without requiring _id.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- deno, mongodb, typescript
- Domain
- api, databases
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100