MongoDB node.js: index created ignoring TTL - node.js

I'm trying to create an index with TTL using the MongoDB driver for Node.js and a Mongo server hosted at mLab.
Node version 9.3.0.
Driver version 3.0.0.rc0
mongod version: 3.4.10 (MMAPv1)
Code in node.js:
var processCollection;
async function init (options) {
processCollection = await options.db.collection('processes');
await processCollection.dropIndexes();
await processCollection.createIndex(
{ 'modified': 1 },
{ expireAfterSeconds: 3600 }
);
}
Results in DB:
db['system.indexes'].find()
{
"v": 2,
"key": {
"modified": 1
},
"name": "modified_1",
"ns": "e-consular.processes"
}
The option expireAfterSeconds is missing in the resulting index. What am I doing wrong?

Collection.createIndex is broken in versions 3.0.0rc0 and 3.0.0 of the Node mongodb driver. It will ignore the options object argument.
This was fixed in version 3.0.1 of the driver. (You can see the fix here).
Update your driver to the latest version (e.g. npm i mongodb#3.0.4) and it should work as expected.

Related

How do I programmatically check the release status of the installed node.js version?

I want to include a check in a setup script that validates the installed node.js version is LTS. Something like this would be ideal
$ node -vvv
v16.3.0
Release v16
Status Active LTS
Codename Gallium
Initial Release 2021-04-20
Active LTS Start 2021-10-26
Maintenance LTS Start 2022-10-18
End-Of-Life 2024-04-30
Basically some way to get the information available on https://nodejs.org/en/about/releases/ but in a script. If there's some HTTP endpoint that provides this stuff in JSON that would work too.
I've looked through available options in the node, npm, nvm, and yarn CLI tools but none of them seem like they can do this.
One source of JSON info about nodejs releases is this endpoint:
https://nodejs.org/download/release/index.json
There, you get an array of objects that each look like this:
{
version: 'v16.14.1',
date: '2022-03-16',
files: [
'aix-ppc64', 'headers',
'linux-arm64', 'linux-armv7l',
'linux-ppc64le', 'linux-s390x',
'linux-x64', 'osx-arm64-tar',
'osx-x64-pkg', 'osx-x64-tar',
'src', 'win-x64-7z',
'win-x64-exe', 'win-x64-msi',
'win-x64-zip', 'win-x86-7z',
'win-x86-exe', 'win-x86-msi',
'win-x86-zip'
],
npm: '8.5.0',
v8: '9.4.146.24',
uv: '1.43.0',
zlib: '1.2.11',
openssl: '1.1.1m+quic',
modules: '93',
lts: 'Gallium',
security: false
},
If you filter that array by the lts property (any value other than false) and then sort by version, you will find the latest LTS release version and you can compare that to what is installed locally.
Here's a piece of nodejs code that gets that JSON, filters out items that aren't lts, then sorts by version. The top of the resulting array will be the latest LTS release version number. You could then programmatically compare that to what is installed locally. You could either just use process.version from within this script or you could run a child_process that captures the output from node -v.
import got from 'got';
// 'v10.16.3'
let versionRegex = /v(\d+)\.(\d+)\.(\d+)/;
// convert version string to a number for easier sorting
function calcVersion(x) {
const match = x.match(versionRegex);
if (!match) {
throw new Error(`version regex failed to match version string '${x}'`);
}
return (+match[1] * 1000000) + (+match[2] * 1000) + (+match[3]);
}
const data = await got("https://nodejs.org/download/release/index.json").json();
const lts = data.filter(item => item.lts);
// for performance reasons when sorting,
// precalculate an actual version number from the version string
lts.forEach(item => item.numVersion = calcVersion(item.version));
lts.sort((a, b) => b.numVersion - a.numVersion);
console.log("All LTS versions - sorted newest first");
console.log(lts.map(item => item.version));
console.log("Info about newest LTS version");
console.log(lts[0]);
console.log(`Newest LTS version: ${lts[0].version}`);
console.log(`Local version: ${process.version}`);
When I run this on my system at this moment, I get this output:
All LTS versions - sorted newest first
[
'v16.14.1', 'v16.14.0', 'v16.13.2', 'v16.13.1', 'v16.13.0',
'v14.19.0', 'v14.18.3', 'v14.18.2', 'v14.18.1', 'v14.18.0',
'v14.17.6', 'v14.17.5', 'v14.17.4', 'v14.17.3', 'v14.17.2',
'v14.17.1', 'v14.17.0', 'v14.16.1', 'v14.16.0', 'v14.15.5',
'v14.15.4', 'v14.15.3', 'v14.15.2', 'v14.15.1', 'v14.15.0',
'v12.22.11', 'v12.22.10', 'v12.22.9', 'v12.22.8', 'v12.22.7',
'v12.22.6', 'v12.22.5', 'v12.22.4', 'v12.22.3', 'v12.22.2',
'v12.22.1', 'v12.22.0', 'v12.21.0', 'v12.20.2', 'v12.20.1',
'v12.20.0', 'v12.19.1', 'v12.19.0', 'v12.18.4', 'v12.18.3',
'v12.18.2', 'v12.18.1', 'v12.18.0', 'v12.17.0', 'v12.16.3',
'v12.16.2', 'v12.16.1', 'v12.16.0', 'v12.15.0', 'v12.14.1',
'v12.14.0', 'v12.13.1', 'v12.13.0', 'v10.24.1', 'v10.24.0',
'v10.23.3', 'v10.23.2', 'v10.23.1', 'v10.23.0', 'v10.22.1',
'v10.22.0', 'v10.21.0', 'v10.20.1', 'v10.20.0', 'v10.19.0',
'v10.18.1', 'v10.18.0', 'v10.17.0', 'v10.16.3', 'v10.16.2',
'v10.16.1', 'v10.16.0', 'v10.15.3', 'v10.15.2', 'v10.15.1',
'v10.15.0', 'v10.14.2', 'v10.14.1', 'v10.14.0', 'v10.13.0',
'v8.17.0', 'v8.16.2', 'v8.16.1', 'v8.16.0', 'v8.15.1',
'v8.15.0', 'v8.14.1', 'v8.14.0', 'v8.13.0', 'v8.12.0',
'v8.11.4', 'v8.11.3', 'v8.11.2', 'v8.11.1', 'v8.11.0',
... 74 more items
]
Info about newest LTS version
{
version: 'v16.14.1',
date: '2022-03-16',
files: [
'aix-ppc64', 'headers',
'linux-arm64', 'linux-armv7l',
'linux-ppc64le', 'linux-s390x',
'linux-x64', 'osx-arm64-tar',
'osx-x64-pkg', 'osx-x64-tar',
'src', 'win-x64-7z',
'win-x64-exe', 'win-x64-msi',
'win-x64-zip', 'win-x86-7z',
'win-x86-exe', 'win-x86-msi',
'win-x86-zip'
],
npm: '8.5.0',
v8: '9.4.146.24',
uv: '1.43.0',
zlib: '1.2.11',
openssl: '1.1.1m+quic',
modules: '93',
lts: 'Gallium',
security: false,
numVersion: 16014001
}
Newest LTS version: v16.14.1
Local version: v16.13.2

How I can start IE in 32bit mode in webdriver.io

I am running an WebDriver.io test using gulp-wdio npm pakage
on selenium-standalone
The Code that I run in gulp is:
gulp.task('e2e', function () {
return gulp.src('wdio.conf.js')
.pipe(wdio({
wdio: {
specs: './test/features/**/*.feature'
}
}));
});
And my wdio.conf.js define browsers this way:
capabilities: [
{
browserName: 'internet explorer',
version: 'ANY'
}
],
How ever the typing is very slow, i had found on the internet that running 32 bit version of the web-driver resolves the issue, how ever I can't find how to configure the capabilities or some other place to run the IE32 bit driver by default...
Any help will be appreciated #:-)
After 2 days of research I had found the solution !!!
There is a configuration file that need to be supplied to the selenium standalone
as shown in this Example
so our final setup is done in this way:
We have a configuration file called wdio.browsers.setup.js that contains the browsers setup:
module.exports = {
baseURL: 'https://selenium-release.storage.googleapis.com',
version: '3.3.1',
drivers: {
chrome: {
version: '2.29',
arch: process.arch,
// - Recent versions of the driver: https://sites.google.com/a/chromium.org/chromedriver/
baseURL: 'https://chromedriver.storage.googleapis.com'
},
ie: {
version: '3.0.0',
arch: 'ia32',
// - Recent versions of the driver: http://selenium-release.storage.googleapis.com/index.html
baseURL: 'https://selenium-release.storage.googleapis.com'
},
firefox: {
version: '0.15.0',
arch: process.arch,
baseURL: 'https://github.com/mozilla/geckodriver/releases/download'
}
}
};
and then inside wdio.conf.js we load it and assign to a special parameters
let browsersSetup = require('./wdio.browsers.setup');
exports.config = {
seleniumArgs: browsersSetup,
seleniumInstallArgs: browsersSetup,
After that all is working fine #:-)
Note: if you have your web-driver installed globally remove the global setup first it's located in:
C:\Users\%USERNAME%\AppData\Roaming\npm
Then you can run the local installation using:
./node_modules/.bin/selenium-standalone install --config=../../wdio.browsers.setup.js
Please find the below working solution for IE browser to install 32 bit:
services: ["selenium-standalone"],
seleniumArgs: {
drivers: {`enter code here`
ie: {
version: "3.4.0", // or whatever latest is
arch: "ia32", // forces use of 32 bit driver
baseURL: "https://selenium-release.storage.googleapis.com"
},
},
},
seleniumInstallArgs: {
drivers: {
ie: {
version: "3.4.0", // or whatever latest is
arch: "ia32", // forces use of 32 bit driver
baseURL: "https://selenium-release.storage.googleapis.com"
},
},
},

Google closure gives error while javascript minificatation

I am using google-closure-compiler grunt task to minify javascript files. I've defined task like : -
'closure-compiler': {
deviceDetails: {
files: {
'target.min.js: 'source.js'
},
options: {
compilation_level: 'SIMPLE'
}
// args: [
// '--js', 'source.js',
// '--compilation_level', 'SIMPLE',
// '--js_output_file', 'out.js',
// '--debug'
// ]
}
This gives me an error
[ { '29': 1,
_state: 2,
_result: [Error: not implemented],
_subscribers: [] } ]
Warning: Compilation error Use --force to continue.
Aborted due to warnings.
Earlier I was facing promise issue , for that I installed pollyfill module.
require('es6-promise').polyfill();
I am running npm 1.3.10 version and Unfortunately, I can't upgrade it right now.
Also , followed alternative approach of using args.. still facing same error.
So after bit analysis, I am using below two grunt plugin's
1. grunt-closure-tools
2. google-closure-compiler
Its was a problem with legacy npm version.

MongoDB production AUTH FAILED

I'm trying to connect my server to my MongoDB database in production,
But When I try to do that :
mongoose.connect("mongodb://user:randompassword#ds011452.mlab.com:11452/dbname?authSource=dbWithUserCredentials");
I get a
connection error: { [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }
note : my dbname is the same as the username...
Any IDeas ?
I've find my answer :
I've upgraded mongoDB to the 3.0 version,
and after that I've upgraded mongoose to the > 4.3.0 version
http://mongoosejs.com/docs/compatibility.html
and It works !
edit :
after update mongoDB to 3.0
(I used brew)
package.json
->
"dependencies": {
"mongoose": "~4.3.0",
...
then npm install

Elastic search error operation [search] and lang [groovy] is disabled?

I am using elastic search 1.7.1 and when i am trying to use script_score or script_fields it is showing the error ScriptException[scripts of type inline], operation [search] and lang [groovy] is disabled can anyone please tell me how can i remove this error. my code is given below
function_score: {
query: {
query_string: {
query: shop_search,
fields: [ 'shop_name']
}
},
functions: [
{
script_score: {
script: "_score * doc['location'].value"
}
}
]
}
Add script.engine.groovy.inline.search: on to elasticsearch.yml configuration file and restart the node.
adding script.groovy.sandbox.enabled: true to .yml works for me
For ES Version 2.x+
script.inline: on
script.indexed: on
Add
script.engine.groovy.inline.aggs: on
script.engine.groovy.inline.update: on
to elasticsearch.yml
and restart
For those with ES 2.x+
script.inline: true
script.indexed: true
Make sure you prefix the lines with a space!

Resources