MATLAB Answers

Creating own "_id" field in MongoDB collection

4 views (last 30 days)
Bálint Décsi
Bálint Décsi on 21 May 2019
Edited: Bálint Décsi on 18 Feb 2020
Descripition
How can I create a valid "_id" field in a struct that MongoDB accepts for its default primary key field? I'm having this trouble when trying to insert a structure into a MongoDB database as a collection using the Database Toolbox Interface for MongoDB. Since MongoDB automatically creates a hexadecimal number for "_id" field for each document even if I give it an "ID" field and I don't want the "_id" to be a hexadecimal (I'd like to provide certain numbers from MATLAB) or having both "_id" and "ID" fields, I should have an "_id" field with the desired value in the structure I'm passing on to the interface.
Errors I got
According to the MATLAB documentation, a valid field name should start with a letter, so when running
cellProps = struct('_id', []);
I get the following error:
Error using struct
Invalid field name "_id".
Steps that did not solve my problem
Checking how MATLAB displays the "_id" field in the Command Window querying a MongoDB database (importing records). It resulted "x_id", and when giving this name to the exported structure's field, MongoDB still creates both "_id" (hexadecimal) and my own "x_id".

  0 Comments

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!