TWO QUESTIONS IN ONE:
Number one: This returns Failing row contains for name column and some more columns when trying to do it in SQL. However I know for a fact that the id allready exsist in the table. So how can I prevent it from giving this error?
Number two: The code returns error: syntax error at or near "$1" when ran in node? Is it beacuse values is a string and how can i prevent this?
This gives error error: syntax error at or near "$1". I have a feeling
let values = ("("+req.body.oldid+","+false+"),("+req.body.newid+","+true+")")
console.log(values) // returns (70,false),(4,true)
const results = await db.query("INSERT INTO practiceSite (id,frontpage) VALUES $1 ON CONFLICT DO UPDATE frontpage = values(frontpage);",[values])
Based on your use case, it looks like you'll only ever be inserting 2 entries at a time, so this should work.
In your example values is seen as a string, so we need to be more granular and pass in each parameter, and create the rows.
const results = await db.query(`
INSERT INTO practiceSite (id, frontpage)
VALUES ($1, FALSE), ($2, TRUE)
ON CONFLICT
DO UPDATE SET frontpage = EXCLUDED.frontpage;
`,
[req.body.oldid, req.body.newid]
)
I have looked at recent posts and nothing on them has worked for me. I have a string set called "friendRequests" in my DynamoDB table and I am trying to append an element to it. However, I keep getting many errors when I try to call db.updateItem with these parameters. Here is my current code, the error is with ExpressionAttributeValues but I have probably spent an hour changing my syntax to no avail.
var params = {
TableName: "users",
Key: { "username": { "S": addFriendInfo.friendUsername } },
UpdateExpression: "SET friendRequests = list_append(friendRequests, :newFriend)",
ExpressionAttributeValues: {
':newFriend': { "SS" : [addFriendInfo.username] }
}
}
That is my code above. addFriendInfo.username / friendUsername are both just strings. This currently gives me a 'Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: SS'. I have tried many things. Can anyone point me in right direction to fixing this damn syntax?
As the DynamoDB documentation explains, DynamoDB has two similar but distinct types - a list and a set. A list is an ordered list of items of any type, while a set is a non-empty, non-ordered collection of items of the same type. The "SS" type you used is a "set of strings". This is distinct from a "list", and you cannot use list_append on sets, as the error message tells you.
To add an element to a set (or create a new set if it doesn't yet exist) you can use the ADD operation, e.g., ADD friendRequests :newFriend.
I have a quick question about mongoose schema real quick. Here is the code: https://i.ibb.co/Db8xPMw/5555.png
I tried to create a document without the property "work". It works in the first time, but it didn't start to work on the second time that I do the same thing again.
Do you have any idea?
Basically I create two documents without an "work" property, which causes a duplicate key error. However, I didn't set up unqiue: true though.
Error :
"errmsg" : "E11000 duplicate key error collection: test.user index work_1 dup key: { : null }
From the message it says your collection has an index with name work_1 probably on field work, Since you've created a document without work field then basically you cannot create another document without work field what so ever in the same collection, cause two documents with no work field or even work field with value as null or same cannot exist as it violates unique constraint policies (it says dup key : { : null}) !! Uniques indexes can be created via mongoose schemas or can also be created by manually running queries on database.
Ref : Search for Unique Index and Missing Field in index-unique
So you need to drop the existing index using dropIndex & then if needed recreate it using createIndex. MongoDB would automatically convert a created index to index-multikey (multi-key index - indexes on array fields) if at least one existing document has array value for that indexed field by the time you create index or even if an array value gets inserted for that field in future.
Through code - Drop index : yourSchema.dropIndex({yourFieldName: 1}) && Create index : yourSchema.index({yourFieldName : 1})
NOTE : Just in case if you want to have certain criteria in unique indexes like situation from this question where indexed field can be missing in some documents but it shouldn't be considered as duplicate insertion, then you can take use of partial-indexes (Search for Partial Index with Unique Constraint) which would only index documents where work field exists.
Ex of partial-indexes :-
db.yourCollectionName.createIndex(
{ work: 1 },
{ unique: true, partialFilterExpression: { work: { $exists: true } } }
)
Ref : mongoose-Indexes
I am running into this bug where if I delete a document from firestore my redux store shows it as null appose to removing it from the store. If I check firestore, I do indeed see it is deleted.
This causes my front-end to break because my .map functions are unable to map null arguments. If could run conditional statements to see if an item in the array is null, then remove this from the array but I wanted to see if there was a more elegant way to handle this bug until it is resolved?
I am using the firetore.data store.
Bug details here
delete does not remove item from data state # 45
https://github.com/prescottprue/redux-firestore/issues/45
I opted to create my own workaround until this bug is resolved.
Create a if statement to check to see if your state has the data. convert this data to an Array and then filter out any undefined data to create your final array.
// Check to see if data exist
if (this.props.userData) {
// Convert data to array
let data = Object.values(this.props.userData);
// Filters undefined objects due to react-redux-firestore bug
data = data.filter(singleData => {
return singleClass !== undefined;
});
}
I will start off by saying while I am not new to CouchDB, I am new to querying the views using JavaScript and the web.
I have looked at multiple other questions on here, including CouchDB - Queries with params, couchDB queries, Couchdb query with AND operator, CouchDB Querying Dates, and Basic CouchDB Queries, just to list a few.
While all have good information in them, I haven't found one that has my particular problem in it.
I have a view set up like so:
function (docu) {
if(docu.status && docu.doc && docu.orgId.toString() && !docu.deleted){
switch(docu.status){
case "BASE":
emit(docu.name, docu);
break;
case "AIR":
emit(docu.eta, docu);
break;
case "CHECK":
emit(docu.checkTime, docu);
break;
}
}
}
with all documents having a status, doc, orgId, deleted, name, eta, and checkTime. (I changed doc to docu because of my custom doc key.
I am trying to query and emit based on a set of keys, status, doc, orgId, where orgId is an integer.
My jQuery to do this looks like so:
$.couch.db("myDB").view("designDoc/viewName", {
keys : ["status","doc",orgId],
success: function(data) {
console.log(data);
},
error: function(status) {
console.log(status);
}
});
I receive
{"total_rows":59,"offset":59,"rows":[
]}
Sometimes the offset is 0, sometimes it is 59. I feel I must be doing something wrong for this not to be working correctly.
So for my questions:
I did not mention this, but I had to set docu.orgId.toString() because I guess it parses the URL as a string, is there a way to use this number as a numeric value?
How do I correctly view multiple documents based on multiple keys, i.e. if(key1 && key2) emit(doc.name, doc)
Am I doing something obviously wrong that I lack the knowledge to notice?
Thank you all.
You're so very close. To answer your questions
When you're using docu.orgId.toString() in that if-statement you're basically saying: this value must be truthy. If you didn't convert to string, any number, other than 0, would be true. Since you are converting to a string, any value other than an empty string will be true. Also, since you do not use orgId as the first argument in an emit call, at least not in the example above, you cannot query by it at all.
I'll get to this.
A little.
The thing to remember is emit creates a key-value table (that's really all a view is) that you can use to query. Let's say we have the following documents
{type:'student', dept:'psych', name:'josh'},
{type:'student', dept:'compsci', name:'anish'},
{type:'professor', dept:'compsci', name:'kender'},
{type:'professor', dept:'psych', name:'josh'},
{type:'mascot', name:'owly'}
Now let's say we know that for this one view, we want to query 1) everything but mascots, 2) we want to query by type, dept, and name, all of the available fields in this example. We would write a map function like this:
function(doc) {
if (doc.type === 'mascot') { return; } // don't do anything
// allow for queries by type
emit(doc.type, null); // the use of null is explained below
// allow queries by dept
emit(doc.dept, null);
// allow for queries by name
emit(doc.name, null);
}
Then, we would query like this:
// look for all joshs
$.couch.db("myDB").view("designDoc/viewName", {
keys : ["josh"],
// ...
});
// look for everyone in the psych department
$.couch.db("myDB").view("designDoc/viewName", {
keys : ["psych"],
// ...
});
// look for everyone that's a professor and everyone named josh
$.couch.db("myDB").view("designDoc/viewName", {
keys : ["professor", "josh"],
// ...
});
Notice the last query isn't and in the sense of a logical conjunction, it's in the sense of a union. If you wanted to restrict what was returned to documents that were only professors and also joshs, there are a few options. The most basic would be to concatenate the key when you emit. Like
emit('type-' + doc.type + '_name-' + doc.name, null);
You would then query like this: key : ["type-professor_name-josh"]
It doesn't feel very proper to rely on strings like this, at least it didn't to me when I first started doing it, but it is a quite common method for querying key-value stores. The characters - and _ have no special meaning in this example, I simply use them as delimiters.
Another option would be what you mentioned in your comment, to emit an array like
emit([ doc.type, doc.name ], null);
Then you would query like
key: ["professor", "josh"]
This is perfectly fine, but generally, the use case for emitting arrays as keys, is for aggregating returned rows. For example, you could emit([year, month, day]) and if you had a simple reduce function that basically passed the records through:
function(keys, values, rereduce) {
if (rereduce) {
return [].concat.apply([], values);
} else {
return values;
}
}
You could query with the url parameter group_level set to 1 or 2 and start querying by year and month or just year on the exact same view using arrays as keys. Compared to SQL or Mongo it's mad complicated and convoluted, but hey, it's there.
The use of null in the view is really for resource saving. When you query a view, the rows contain an _id that you can use in a second ajax call to get all the documents from, for example, _all_docs.
I hope that makes sense. If you need any clarification you can use the comments and I'll try my best.