Getting Error createIndex not a function

Below is my code of Function on MongoDB it is not working & throwing Error as createIndex() is not a function.
exports = function(arg){
const db = context.services.get(“mongodb-atlas”).db(“myapp”).collection(‘event’);

// return db.insertOne({‘DateTime’: new Date(),‘title’:‘Hello’});
return db.createIndex( { “DateTime”: 1 }, { “expireAfterSeconds”: 0} );
};

Hi @Its_Me,
Can you add this line before the return statement and tell us what you get?

console.log(db);

Thanks,
Rafael,

This is the Error i am getting

ran on Mon Jun 28 2021 13:28:42 GMT+0530 (India Standard Time)
took 262.782947ms
error:
TypeError: ‘createIndex’ is not a function
trace:
TypeError: ‘createIndex’ is not a function
at exports (function.js:20:26(34))
at function_wrapper.js:3:1(21)
at :11:8(4)
at :2:15(7)

logs:
[object Object]

Hi,
Can you change

console.log(db)

to

console.log(JSON.stringify(db))

?
Thanks,
Rafael,

This is what is got

ran on Mon Jun 28 2021 13:34:27 GMT+0530 (India Standard Time)
took 278.613746ms
error:
TypeError: ‘createIndex’ is not a function
trace:
TypeError: ‘createIndex’ is not a function
at exports (function.js:20:26(38))
at function_wrapper.js:3:1(21)
at :11:8(4)
at :2:15(7)
logs:
{}

does this command work for you?

db.insertOne({‘DateTime’: new Date(),‘title’:‘Hello’});

Yes its works
> ran on Mon Jun 28 2021 13:41:43 GMT+0530 (India Standard Time)

took 306.704738ms
logs:
{}
result:
{
“insertedId”: {
“$oid”: “60d98444ef2924e558dec08c”
}
}
result (JavaScript):
EJSON.parse(‘{“insertedId”:{“$oid”:“60d98444ef2924e558dec08c”}}’)

I’m not familiar with MongoDB Realm so I don’t know if it’s even possible to create an index that way.
You can try creating the index using the mongodb shell (or in atlas).
Goodluck,
Rafael,

no problem thanks you so much

Hi @Its_Me,

createIndex() is not a part of the MongoDB Actions available in functions for Realm.

As Rafael suggested, you can create the index using mongo shell or on Atlas using Data Explorer.

Hope this helps.

Kind Regards,
Jason