Model.base.modelSchemas undefined when upgrading to mongoose 6 - node.js

We’ve been working with mongoose#^5.4.11 and due to the MongoDB Server upgrade to version 5, we are upgrading to mongoose#6.1.7.
In our code we have this line:
Model.base.modelSchemas[modelName].paths
after upgrading to 6.1.7 I get undefined for modelSchemas.
I cloned the mongoose repo for tag 5.4.11, and there modelSchemas is a property of the Mongoose class in ‘lib/index.js/’
I also cloned the mongoose repo for tag 6.1.7, and there modelSchemas is not defined anywhere (only see mentions of it in docs files)
In the mongoose migration guide 5 to 6 modelSchemas is not mentioned anywhere.
I think I have things working again by changing the code to:
Model.base.model(modelName).schema.paths
Could someone confirm this change is exactly equivalent to the original statement?

Related

Typescript is getting mad when I try to create Mongodb documents with a "manual _id"

I'm using Mongodb Driver for Node and I'm defining an _id property manually using Nanoid package (which creates an string id) to a specific collection.
Typescript is getting mad with me when I try to create a new document because Mongodb Driver defines that the _id property needs an ObjectId type, but it is getting a Nanoid string.
Anyone knows how to make typescript happy at this situation?

Is it valid to use 'Object' as mongoose schema type?

I have faced some issues storing a json object inside mongodb with mongoose .
My debugging lead to the mongoose schema{making changes in the schema made it work but not as desired} .
So i went online and looked at how to save json object to a document, I've found many of answers regarding saving an object with fixed properties but i couldn't find many answeres about saving an object with changing properties . Some answeres here on 'stack overflow' used Object as schema type but those answeres were out of context so there wasn't enough information about it to know if it's valid or not .
It's very important to mention that on the mongoose documentation Object is not a schema type .
mongoose.Schema({key: {type: Object}});
Yes it is as far as I know. I always use it and never got any problems.

How to watch for an updated boolean value in a pymongo document?

I am trying to listen for an update to a collection using pymongo 3.6.1.
The collection gets updated with a document that looks something like this:
{"End_Word":"bit","Success":true,"Score":1,"Term_Index":5}
Where if the key Success gets updated to true in any of the documents a def dosomething() gets called.
The mongodb api documentation shows this example, as a first step:
with db.collection.watch() as stream:
for change in stream:
print(change)
In attempting to replicate this I am getting a OperationFailure: Unrecognized pipeline stage name: '$changeStream' error. When researching other's with similar issues on stack The answer was connection driver versions. I don't think this is the case here:
Code:
db_name = 'mapstore'
coll_name = 'oxygen'
MONGO_DB_DRIVER = pymongo atlas M2 instance driver
conn = pymongo.MongoClient(MONGO_DB_DRIVER)
db = conn[db_name]
print(db.collection_names())
Update:
I also looked in these places
How to listen for changes to a MongoDB collection?
https://docs.mongodb.com/master/changeStreams/
What is the right approach?
The below error
OperationFailure: Unrecognized pipeline stage name: '$changeStream'
Indicates that your Mongodb server is below 3.6.0 and that is why it doesn't recognize this command. Even though you have latest client, doesn't change how the server behaves

group by in Flask-MongoAlchemy

i stared new project with flask web-framework with mongoDB. i also used database and access data using Flask-MongoAlchemy. i tried work on different query like .all(), .filter_by(), .get() its work nice. but problem is that how to use aggregate funcation in Flask-MongoAlchemy ? for example i want to use group_by.
i tried following but its still not working.
db.User.qroup_by(name)
its gives following error
'BaseQuery' object has no attribute 'group_by'

mongoose findById would not work with correct Id

I was creating a database storing user information. Previously, the system worked well, after I performed npm update of mongoose, the system failed and I dont know why. The following is the error message I encountered.
CastError: Cast to ObjectId failed for value "xxxxxxxxxxxxxxx" at path "_id" for model "User"
Then, I write some simple testing code to test if the database is connected successfully. For example:
User.findOne({firstName: "David"}, function(err, user){console.log(user.lastName)});
The above worked perfectly.
I then found the corresponding Id from the database directly and create variable like:
var testId = "abcde"
and tried the following code:
User.findById(testId, function(err, user){console.log(user.firstName)});
Then it just output the error messages.
I have tried mongoose.Types.ObjectId.isValid('abcde') (abcde is just example) and it returned true. Therefore, I really have no ideas why it did not work...is it a mongoose bug?
Please help if you know the answer, thank you very much!
I just solved the problem.
As I mentioned, the findById function did not work after I updated mongoose. I was struggling on the code for few days and when I woke up this morning, I suddenly thought of upgrading my node.js.
That worked.
After upgrading node.js from v4.x.x to latest version, everything worked fine : )
This taught me a lesson that I should not upgrade a single module alone and care of compatibility issue.

Resources