Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Unable to save timeseries document: lock for timeseries is not allowed #1005

Open
tyrantlink opened this issue Aug 19, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@tyrantlink
Copy link

tyrantlink commented Aug 19, 2024

Describe the bug
Document.save() fails when using a timeseries collection, returning pymongo.errors.OperationFailure: Taking test.timeseries_test lock for timeseries is not allowed, full error:

{'ok': 0.0, 'errmsg': 'Taking test.timeseries_test lock for timeseries is not allowed', 'code': 166, 'codeName': 'CommandNotSupportedOnView'}

To Reproduce
with beanie==1.26.0, pymongo==4.8.0, and motor==3.5.1 on python 3.11.5

from datetime import datetime
from asyncio import run

from beanie import init_beanie, Document, TimeSeriesConfig
from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import Field


MONGO_URI = ''
DATABASE = ''


class TimeSeriesTest(Document):
    ts: datetime = Field(default_factory=datetime.now)

    class Settings:
        name = 'timeseries_test'
        timeseries = TimeSeriesConfig(
            time_field='ts',
            expire_after_seconds=60
        )


async def main():
    _client = AsyncIOMotorClient(
        MONGO_URI,
        serverSelectionTimeoutMS=5000
    )[DATABASE]

    await init_beanie(_client, document_models=[TimeSeriesTest])

    ts_test = TimeSeriesTest()

    await ts_test.save()


if __name__ == '__main__':
    run(main())

Expected behavior
Document is successfully inserted

Additional context
The code above does successfully insert when not using a timeseries collection

@adeelsohailahmed adeelsohailahmed added the bug Something isn't working label Aug 21, 2024
@szyg
Copy link

szyg commented Dec 30, 2024

Am having the same issue. Is there any recommended work around other than just converting to a non time series collection?

@tyrantlink
Copy link
Author

For using a time series collection, I wasn't able to find one, however for my specific use-case, just a regular collection with a TTL index fit my needs, although it's less efficient.

@baum314nk
Copy link

baum314nk commented Jan 6, 2025

You must not use the save method as it passes upsert=True to the underlying pymongo function which is not supported (see here). Instead use insert. I think this should be added in the docs related to time series

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants