简体   繁体   English

编写 mongodb 文档时如何忽略 _id 和 _class?

[英]how to ignore _id and _class when write mongodb document?

I have one collection which stores time-series data, only insert and query, no update and delete, as MongoTemplate genenrates _id and _class fields automatically, these two fields consume disk space and I don't need them, how can I ignore them when writing?我有一个存储时间序列数据的集合,只插入和查询,没有更新和删除,因为MongoTemplate会自动生成_id_class字段,这两个字段占用磁盘空间,我不需要它们,我怎么能忽略它们写作?

PS: I know how to ignore _class globally, how to ignore _class only for one specific collection? PS:我知道如何全局忽略 _class,如何仅针对一个特定集合忽略 _class?

I'm not sure why you would want to enable the global _class & _id and disable it only certain conditions.我不确定您为什么要启用全局 _class 和 _id 并仅在某些条件下禁用它。

Anyway for the problem, if you want to discard those fields, you can just convert your POJO to a raw json and directly save it in Mongo.无论如何,如果您想丢弃这些字段,您可以将您的 POJO 转换为原始 json 并直接将其保存在 Mongo 中。

Document document = Document.parse(jsonString)
mongoTemplate.getCollection("collection").insertOne(document);

You can create a wrapper write method to incorporate these changes.您可以创建包装器写入方法来合并这些更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 mongoDB 文档映射到 C# 类时如何处理 id 元素 - How to handle id element when mapping mongoDB document to C# class 如何为具有嵌套属性的 mongodb 文档编写 springboot class? - How do you write a springboot class for a mongodb document with nested properties? 如何在解组 MongoDB 文档时忽略空值? - How to ignore nulls while unmarshalling a MongoDB document? 在MongoDB中创建文档时如何用Mongoose指定_id字段? - How to specify the _id field with Mongoose when creating a document in MongoDB? MongoDB 指南针:插入文档时如何为嵌套对象生成“_id” - MongoDB Compass: How to generate "_id" for nested objects when inserting a document 例如,当想要使用 findById 时,如何访问文档的 MongoDB _id? - How to access MongoDB _id of a Document when wanting to use findById for instance? 如何防止MongoDB在查找文档时返回对象ID? - How to prevent MongoDB from returning the object ID when finding a document? 忽略 mongodb 中的特定文档 - ignore a specific document in mongodb 如何使用mongodb查询生成器编写Java代码以使用“ _id”检索文档? - How to write java code using mongodb query builder to retrieve a document using “_id”? _id为对象时,在MongoDb中更新文档 - Update document in MongoDb when _id is an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM