
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Create ObjectId in MongoDB using a seed string?
The ObjectId does not accept the seed string. You need to use _id − StringValue. Let us create a collection with documents −
> db.demo667.insertOne({_id:"Chris"}); { "acknowledged" : true, "insertedId" : "Chris" } > db.demo667.insertOne({_id:"David"}); { "acknowledged" : true, "insertedId" : "David" } > db.demo667.insertOne({_id:"Chris"}); 2020-04-23T22:01:23.268+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo667 index: _id_ dup key: { : "Chris" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.demo667 index: _id_ dup key: { : \"Chris\" }", "op" : { "_id" : "Chris" } }) WriteError@src/mongo/shell/bulk_api.js:461:48 Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:841:49 Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13 Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21 DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9 @(shell):1:1 > db.demo667.insertOne({_id:"Bob"}); { "acknowledged" : true, "insertedId" : "Bob" } > db.demo667.insertOne({_id:"Mike"}); { "acknowledged" : true, "insertedId" : "Mike" }
Display all documents from a collection with the help of find() method −
> db.demo667.find();
This will produce the following output −
{ "_id" : "Chris" } { "_id" : "David" } { "_id" : "Bob" } { "_id" : "Mike" }
- Related Articles
- Convert string to objectid in MongoDB?
- How to convert ObjectId to string in MongoDB
- MongoDB query to convert from ObjectId to String
- Find a document with ObjectID in MongoDB?
- How to generate ObjectID in MongoDB?
- Display MongoDB records by ObjectId?
- MongoDB query to update a MongoDB row with only the objectid
- Cast to ObjectId failed for value in MongoDB?
- How to return documents of a collection without objectId in MongoDB?
- Use ObjectId under findOne() to fetch a specific record in MongoDB?
- How we can perform sort on ObjectId column in MongoDB?
- How to create a database in MongoDB using Java?
- How to create a MongoDB collection using Java?
- How to create an index in MongoDB using Java?
- Create index in a MongoDB collection?

Advertisements