为什么 MongoDB 子文档过期会删除父文档?

Why MongoDB subdocument expiration removes parent document?

我使用 mongoose 5 并且有这样的模式:

user.js

const mongoose = require('mongoose');
const GeoData = require('./geodata');

const UserSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  createdAt: {
    type: Date,
    default: Date.now()
  },
  geodata: GeoData
});

UserSchema.index({ deviceToken: 1 }, { unique: true });

module.exports = UserSchema;

geodata.js

const mongoose = require('mongoose');
const c2p = require('circle-to-polygon');

const GeoDataSchema = new mongoose.Schema({
  location: {
    coordinates: [Number],
    type: {
      type: String
    }
  },
  createdAt: {
    type: Date,
    default: Date.now()
  },
  expireAt: {
    type: Date,
    default: new Date().setHours(12,0,0,0)
  }
});

GeoDataSchema.index({ location: "2dsphere", bounds: "2dsphere" });
GeoDataSchema.index({ 'expireAt': 1 }, { expireAfterSeconds: 0 });

module.exports = GeoDataSchema;

假设 geodata 子文档被添加到父 user 文档,默认情况下过期时间设置为 12:00:00 当地时间。 不幸的是,这会删除带有子文档 geodata 的父 user,而不是像我期望的那样仅删除 geodata

这是正常行为还是我遗漏了什么?

TTL 索引始终适用于根文档,从不适用于单个子文档或文档的其他部分。

根据 documentation:

A special TTL index property supports the implementation of TTL collections. The TTL feature relies on a background thread in mongod that reads the date-typed values in the index and removes expired documents from the collection.