Meteor provides two MONGODB databases: A client-side cache database and a MONGODB database on the server. When a user changes some data (for example, by clicking Save), the JavaScript code that runs in the browser updates the corresponding database entry in the local MongoDB and then sends a DDP request to the server. The code immediately continues to run as if the operation had been successful because it does not need to wait for the server to reply. At the same time, the server is updated in the background. If the server operation fails or returns an unexpected result, the client-side JavaScript code is adjusted immediately based on the newly returned data from the server.
When I wrote the code yesterday, I found that there was an update operation that always failed, and there was nothing wrong with it, just an error. Looking for a long time to find out where the problem is.
Then read the official documentation, which is written like this:
Description of the update in the documentation:
Collection.update (selector, modifier, [options], [callback]) Anywhere
Modify one or more documents in the collection
Arguments
selector Mongo selector, or object ID
Specifies which documents to modify
modifier Mongo modifier
Specifies how to modify the documents
Callback Function
Optional. If present, called with an error object as its argument.
Options
Multi Boolean
True to modify all matching documents; false to only modify one of the matching documents (the default).
Look again at the code I wrote, should be correct. And then the MongoDB command-line tool to try to execute the same statement is also possible, so I think it is meteor related setup issues.
Take a closer look at the document and find the problem:
The behavior of update differs depending on whether it was called by trusted or untrusted code. Trusted code includes server code and method code. Untrusted code includes client-side code such as event handlers and a browser ' s JavaScript console.
Trusted code can modify multiple documents at once by setting multi to True, and can use a arbitrary Mongo selector to fi nd the documents to modify. It bypasses any access control rules set to allow and deny.
Untrusted code can only modify a single document at once, specified by its _id. The modification is allowed only after checking any applicable allow and deny rules.
Originally for the sake of security, Meteor limited to the client under the default operation of the database, insert,update,remove operations such as selector can only be _id, and can not use mutil, etc. So the error in the UPDATE statement I wrote was that selector used conditions outside the _id. So there are two ways to solve it:
- The client simply uses _id as the selector operation, which is obviously inconvenient.
- Write allow rules on the server side, allowing clients to perform these operations directly on the database
Description of allow in the documentation:
Collection.allow (Options) Server
Allow users to write directly to the collection from client code, subject to limitations you define.
Options
INSERT, UPDATE, remove Function
Functions that look at a proposed modification to the database and return true if it should be allowed.
So write a relevant allow rule, let him return to true in some cases
Meteor Update and other operation failure reasons and solutions