I am using Nodejs based mongoskin driver for mongo database operation. I want to update my document however don't want to update few fields. Following are more details.
Request for add:
{
"name": "Theme Name",
"description": "Theme Description",
"createdByUserId": "53651221b25521601a5c9530",
}
Request for update:
{
"_id":"53555ef203dabf282b750a81"
"name": "Theme Name",
"categoryId": "53555ef203dabf282b750a81",
"description": "Theme Description",
"createdByUserId": "53651221b25521601a5c9530",
"updatedByUserId": "5675561b25521601a5c9530",
"dateCreated": ISODate("2014-05-19T19:47:26.603Z"),
"dateUpdated": ISODate("2014-05-19T19:49:28.203Z"),
}
I want to ignore following field send by client.
1. createdByUserId
2. dateCreated
For time being I am taking following approach in update operation:
1. Read collection for given _id
2. Read these above two fields from database and update the request and then perform database update operation
Looking help for clean approach.
Your request for update actually does the following: it replaces everything in the document with the values provided by the request (except for the "_id" of course, which is immutable). What you want is what is called a "partial update" in mongosphere. Please have a look into the $set operator. So what you would do is something like:
db.yourcollection.update({"_id":"53555ef203dabf282b750a81"},
{$set:
{
"categoryId":"53555ef203dabf282b750a81",
"updatedByUserId":"5675561b25521601a5c9530",
"dateUpdated":ISODate("2014-05-19T19:49:28.203Z")
}
}
)
As far as I know there is now way of sending a complete document to a mongo[s|d] and tell it to only skip certain fields.
Related
Users can press a button on my website to declare interest in a course. For every course there is a document in my CouchDB installation. These documents look like this:
{
"_id": "...",
"_rev": "...",
"name": "...",
"description": "...",
"userList": []
}
When a users presses the button his name should be added to "userList". I wrote a Design Document for this:
{
"_id": "_design/updateList",
"_rev": "...",
"updates": {
"addUser": "function(doc, req) {doc['userList'] = req['name']; var message = 'user added'; return [doc, message];}",
}
}
I know that this cannot be the right solution because the list can never be longer than one user name like this. However, not even that works. When I press on the button, the line ""userList": []" disappears from the corresponding document.
What's the problem here? I use PHP-On-Couch to run the Design Document but there shouldn't be any problems in my PHP code. I see in the CouchDB log that CouchDB receives the user name just fine.
The direct problem is that a req object does not have a field called "name" so:
doc['userList'] = req['name']
is equivalent to:
doc['userList'] = undefined
you may have meant to use userCtx req.userCtx.name directly if users are logged in, or req.query.name if you have added it as a parameter.
More generally, you probably meant to push their name on to the array which is probably fine if class sign up isn't very high volume. An alternate approach is to generate an independent document for each user+class and rely on views to count them.
How to remove __table from Tabel Knex.js select query.
The select query returns all data i queried +__table but how can i remove __table.
My select query
table('TestSeriesModes').select('ID', 'Name', 'IsActive', 'IsPhysicalInventory', 'Code').orderBy('ID', 'asc').all()
Returning data
{
"__table": "TestSeriesModes",
"ID": 1,
"Name": "Online",
"IsActive": true,
"IsPhysicalInventory": true,
"Code": "ON"
},
{
"__table": "TestSeriesModes",
"ID": 2,
"Name": "Calling Tablet",
"IsActive": true,
"IsPhysicalInventory": true,
"Code": "CALTAB"
}
Knex doesn't even have that .all method (at least officially). Looks like you are not using knex directly here. Knex never adds anything like you have described (key called __table) to your resulting rows. Complete reproduction case would be useful, because now the post does not have enough code shown to be able to tell what is wrong with it.
To answer the query, you can for example use knex directly to get results without that key. Maybe you are using some ORM, which adds it or some other library.
Other way to drop it would be to filter it out from results afterwards for example by doing:
const filteredResults = results.map(res => {
delete res.__table;
});
Or to use some library like ramda / lodash to filter extra keys from resulting objects.
I'm building a rest api that allows users to submit and retrieve data produced by surveys: questions are not mandatory and each "submit" could be different from each other. Each submit is a json with data and a "survey id":
{
id: abc123,
surveyid: 123,
array: [],
object: {}
...
}
I have to store this data and allow retrieving and querying.
First approach: going without schema and putting everything in a single collection: it works, but each json field is treated as a "String" and making queries on numeric values is problematic.
Second approach: get questions datatypes for each survey, make/save a mongoose schema on a json file and then keep updated this file.
Something like this, where entry "schema : {}" represent a mongoose schema used for inserting and querying/retrieving data.
[
{
"surveyid" : "123",
"schema" : {
"name": "string",
"username" : "string",
"value" : "number",
"start": "date",
"bool" : "boolean",
...
}
},
{ ... }
]
Hoping this is clear, I've some questions:
Right now I've a single collection for all "submits" and everything is treated as a string. Can I use a mongoose schema, without other modifications, in order to specify that some fields are numeric (or date or whatever)? Is it allowed or is it even a good idea?
Are there any disadvantage using an external json file? Mongoose schemas are loaded at run time when requested or does the service need to be restart when this file is updated?
How to store data with a "schema" that could change often ?
I hope it's clear!
Thank you!
EDIT: we have just heard back from Microsoft that using the mongodb api against a "traditional" documentdb instance isn't supported. I'll leave this question open until someone comes up with a custom solution or Microsoft enhances the product.
We have successfully connected to our existing DocumentDB instance using the mongodb API, but the find() method does not return the id field inside the results.
The examples on the Query Tutorials page show an id field being returned, but they also show an "_id": "ObjectId(\"58f65e1198f3a12c7090e68c\")" field appended with no explanation as to how that 58f6... string is generated (it doesn't appear anywhere in the sample document at the top of the page).
For example, we can find this document using the query find({"type":"attribute", "association.Season":"Spring"}):
{
"id": "ATTR-TEST-ATT-00007",
"type": "attribute",
"name": "Spring Football",
"association": {
"Season": "Spring"
}
}
...but the mongodb API leaves out the id property from the document, so all we see is:
{
"type": "attribute",
"name": "Spring Football",
"association": {
"Season": "Spring"
}
}
Also, if we use find({"id": "ATTR-TEST-ATT-00001"}) nothing gets returned even though there is a document with that id in the database.
We have tried using the project argument to force inclusion of the id field, but it didn't work.
If we add an _id field to the document then the mongodb driver returns the _id field without needing a projection, but this is not a solution as we want to use both the DocumentDB SQL API and the mongodb API at the same time.
{
"id": "ATTR-TEST-ATT-00007",
"_id": "ATTR-TEST-ATT-00007",
"type": "attribute",
"name": "Spring Football",
"association": {
"Season": "Spring"
}
}
How do we get the mongodb API to return the document's id field in queries?
Pardon my ignorance if it is a very basic question.
ABC collection in MongoDB has the following schema.
{
"metadata": {
"store": 5051,
"catg": "XYZ",
},
"category": {
"name": "XYZ",
"id": "CL141778",
}
}
I need to query (where "metadata.catg" == "category.name")
What is the best way to do it without using mongoose dbref ?
In MongoDB you can not compare two fields values with each other in a query. What I expect is that you need to redesign your schema. You need to look at how you query (and update) as supposed to thinking MongoDB is yet another RDBMs.
Show us the real schema, some example documents and a few query types that you need to run and I can see if I can help.