Skip to content

Commit adc9bb5

Browse files
authored
fix(db-mongodb): docs duplicated in list view with drafts (#8435)
fixes #8430
1 parent a09811f commit adc9bb5

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

packages/db-mongodb/src/createVersion.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ObjectIdImport from 'bson-objectid'
2+
import { isValidObjectId } from 'mongoose'
13
import {
24
buildVersionCollectionFields,
35
type CreateVersion,
@@ -10,6 +12,8 @@ import type { MongooseAdapter } from './index.js'
1012
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js'
1113
import { withSession } from './withSession.js'
1214

15+
const ObjectId = (ObjectIdImport.default ||
16+
ObjectIdImport) as unknown as typeof ObjectIdImport.default
1317
export const createVersion: CreateVersion = async function createVersion(
1418
this: MongooseAdapter,
1519
{
@@ -47,6 +51,23 @@ export const createVersion: CreateVersion = async function createVersion(
4751

4852
const [doc] = await VersionModel.create([data], options, req)
4953

54+
const parentQuery = {
55+
$or: [
56+
{
57+
parent: {
58+
$eq: data.parent,
59+
},
60+
},
61+
],
62+
}
63+
if (typeof data.parent === 'string' && isValidObjectId(data.parent)) {
64+
parentQuery.$or.push({
65+
parent: {
66+
$eq: ObjectId(data.parent),
67+
},
68+
})
69+
}
70+
5071
await VersionModel.updateMany(
5172
{
5273
$and: [
@@ -55,11 +76,7 @@ export const createVersion: CreateVersion = async function createVersion(
5576
$ne: doc._id,
5677
},
5778
},
58-
{
59-
parent: {
60-
$eq: data.parent,
61-
},
62-
},
79+
parentQuery,
6380
{
6481
latest: {
6582
$eq: true,

0 commit comments

Comments
 (0)