MongoDB/Express Query Object from URL - node.js

Sorry in advance for what I'm sure is going to be really straightforward. My brain is just not seeing it.
I have a database. I'd like to query that database from the frontend, using the arguments in my URI. We're not at that part yet. Express/Node on the back.
When I use this query:
dbo.collection("archive").find({OBJECT: 'RRP68'},{ projection: {
I get some results, as expected, of entries featuring that object. When I try this:
var query = {OBJECT: 'RRP68'};
dbo.collection("archive").find({query},{ projection: {
I get no results. Initially I thought the problem was how I consumed the URI element, then I thought scoping (even though pagination works fine in the same manner), but this is the most simplified version I could come up with that still didn't work. So clearly there's some syntax or structure concept or something along those lines I don't know, and can't seem to find in the docs.
Pointers would be appreciated.

It's because this is what is happening right now
dbo.collection("archive").find({{OBJECT: 'RRP68'}},{ projection: {
Do you see it? It's because of double curly brackets
This Will fix it
var query = {OBJECT: 'RRP68'};
dbo.collection("archive").find(query,{ projection: {

Related

nodejs bigquery parameterised query inside IN() expression

I am trying to run a parameterised query using the npm module #google-cloud/bigquery.
Something like this:
SELECT * FROM myTable WHERE id IN (#ids);
I have no idea how bigQuery is expecting the parameter ids formatted.
My options.params look like something like this:
{ ids: '"1234", "4567"'}
But I don't get any result back. I know there are results, I can see them in bigquery and if I remove the parameter and just inject the string works just fine.
It seem pretty easy, but I can't figure out why it doesn't work, anyone who is willing to help me out?
Thank you in advance
Of course I found the solution as soon as I posted the question...
Thanks to this thread! Need to do some gymnastic...
So provided that the parameter is a string like:
'1234,5678'
We need to do:
WHERE id IN UNNEST(REGEXP_EXTRACT_ALL(#ids,"[0-9a-zA-Z]+"))
REGEXP_EXTRACT_ALL - returns an array
UNNEST - flattens the array for the IN clause as stated in the link above.

How do I input a NodeJS variable as a parameter in a MongoDB Query

I've read through every stack overflow I can find and I don't understand why this still isn't working.
I'm trying to construct a NodeJS Mongo find query and very simply want to use a variable as the values, the key does not need to be dynamic.
This is the code I was working with initially :
collection.find({project_id : project_id_val})
but this simply returns :
Found the following records
[]
I've also tried constructing my own javascript object and passing that in e.g.
Query = {}
Query["project id"] = project_id_val
collection.find(query)
But that doesn't work either, I know the key/value pair is correct because
project_id: "12345" works absolutely fine, and returns exactly what I want it to. I feel like this should be very simple so if someone could let me know where I'm going wrong that would be great.
Thanks.

Marklogic QueryByExample in collection NodeJS

TLDR
Is there a way to limit queryByExample to a collection in NodeJS?
Problem faced
I have a complex query with some optional fields (i.e. sometimes some search fields will be omitted). So I need to create a query dynamically, e.g. in JSON. QueryByExample seems to be the right tool to use here as it gives me that flexibility to pass a JSON. However my problem is that I would like to limit my search to only one collection or directory.
e.g. I was hoping for something like
searchJSON = {
title: { $word: "test" },
description: { $word: "desc" }
};
//query
db.documents.query(qb.where(
qb.collection("collectionName"),
qb.byExample(searchJSON)
)).result()...
In this case searchJSON could have been built dynamically, for example maybe sometimes title may be omitted from the search.
This doesn't work because the query builder only allows queryByExample to be the only query. But I'd instead like to built a dynamic search query which is limited to a collection or directory.
At present, I think you would have to express the query with QueryBuilder instead of Query By Example using
qb.and([
qb.collection('collectionName'),
qb.word('title', 'test'),
qb.word('description', 'desc')
])
See http://docs.marklogic.com/jsdoc/queryBuilder.html#word
That said, it should be possible for the Node.js API to relax that restriction based on the fixes in MarkLogic 9.0-2
Please file an issue on https://github.com/marklogic/node-client-api

KnexJS Raw Method doesn't work in one case

Working with KnexJS on a project and have been using the .raw() method throughout the project with out a single issue.
I now have one case where the .raw is just not being built into the SQL, and so I end up with the syntax error at end of input error. If I dump the SQL string, I can see why as it's failing
'update "mytable" set "a" = ? returning '
I can see why it's having an issue, the problem I'm having is that the returning is a Raw value as such, and so I just can't figure out why it's not being compiled with the SQL.
knexQuery.update(data).into('mytable').returning( knexQuery.raw('mytable::json') );`
If I use a string, in place of the raw in the returning() method, it will compile the string into it.
If I console out the knexQuery.raw('mytable::json') part, it shows as a Raw object, with the right object data...
Raw {
client:
Client_PG {
... },
sql: 'mytable::json',
bindings: undefined,
_wrappedBefore: undefined,
_wrappedAfter: undefined,
_debug: undefined } }
I know that the SQL works, as I've filled in the missing part and it works, but the SQL is not the issue, I just can't figure out why the raw() method is not being compiled with the SQL string.
I also have another piece of code (and INSERT one) somewhere else in my code that is using the same returning( knexQuery.raw(....) ) and that works perfectly fine.
I'm starting to think this is a bug in the code, but after spending an hour going through the KnexJS library code, I can't see any reason why it would not work.
So why does this code not build the raw into the query, while my other one has no problem and works?
I'm getting to the point where I just want to use something else to get around the problem, but it's just not possible without using this method.
This is bug in knex https://runkit.com/mikaelle/592412c3a631940012a51928 please open issue in knex github. Looks like handling raw input just haven't been implemented to returning builder method.
Anyways looks like this works as a workaround:
knex('TestTable')
.insert({ foo: 'bar' })
.returning([knex.raw('mytable::json')]) // raw wrapped in array
.toSQL();

DocumentDB Replace not Working

I recently realized that DocumentDB supports stand alone update operations via ReplaceDocumentAsync.
I've replaced the Upsert operation below with the Replace operation.
var result = _client
.UpsertDocumentAsync(_collectionUri, docObject)
.Result;
So this is now:
var result = _client
.ReplaceDocumentAsnyc(_collectionUri, docObject)
.Result;
However, now I get the exception:
Microsoft.Azure.Documents.BadRequestException : ResourceType Document is unexpected.
ActivityId: b1b2fd71-3029-4d0d-bd5d-87d8d0a2fc95
No idea why, upsert and replace are of the same vein and the object is the same that worked for upsert, so I would expect it to work without problems.
All help appreciated.
Thanks
Update: Have tried to implement this using the SelfLink approach, and it works for Replace, but selflink does not work with Upsert. The behavior is quite confusing. I don't like that I have to build a self link in code using string concatenation.
I'm afraid that building the selflink with string concatenation is your only option here because ReplaceDocument(...) requires a link to the document. You show a link to the collection in your example. It won't suck the id out and find the document as you might wish.
The NPM module, documentdb-utils, has library functions for building these links but it's just using string concatenation. I have seen an equivalent library for .NET but I can't remember where. Maybe it was in an Azure example or even in the SDK now.
You can build a document link for a replace using the UriFactory helper class:
var result = _client
.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, docObject.Id), docObject)
.Result;
Unfortunately it's not very intuitive, as Larry has already pointed out, but a replace expects a document to already be there, while an upsert is what it says on the tin. Two different use-cases, I would say.
In order to update a document, you need to provide the Collection Uri. If you provide the Document Uri it returns the following:
ResourceType Document is unexpected.
Maybe the _collectionUri is a Document Uri, the assignment should look like this:
_collectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName);

Resources