$ne not working in MongoDB query - feathersjs

My MongoDB query isn't filtering by $ne on the FeathersJS client plugin. Location/$near is working.
app.service('users').find({
query: {
location: {
$near: {
$geometry: {type: "Point", coordinates: [user.location.coordinates[0], user.location.coordinates[1]]},
$minDistance: 0,
$maxDistance: 20000
}
},
_id: {
$ne: user._id
},
profileSetup: true
}

Normally in Mongo to compare ObjectIDs you need to wrap your value (user._id) with ObjectId, importing it from Mongo.
const ObjectID = require("mongodb").ObjectID
app.service('users').find({
query: {
location: {
//... geo query here
_id: {
$ne: ObjectID(user._id)
},
profileSetup: true
}
The reason is that an objectId is not a string internally, I guess. :)

Related

BAD_INPUT for update version API in Autodek Forge

I'm trying to upload file to BIM360DOCS. Referring this I could successfully upload the first version of file, but I'm facing issues while updating the version. For updating version of file, I created a storage location, uploaded a file to the storage location, But I try to update the version, I'm getting 400 in response.
I'm referring this method.
const body: CreateVersion = {
jsonapi: { version: "1.0" },
data: {
type: "versions",
attributes: {
name: fileName,
extension: {
type: versions:autodesk.bim360:File" // "versions:autodesk.core:File",
version: "1.0",
schema: {
href: ""
}
}
},
relationships: {
item: {
data: {
type: "items",
id: folderId
}
},
storage: {
data: {
type: "objects",
id: objectId
}
}
}
}
}
const version = await new VersionsApi().postVersion(projectId, body, internalTokenClient, internalToken)
But this gives me 400 ERROR saying
errors: [
{
id: '92######-####-####-####-######c98bb5',
status: '400',
code: 'BAD_INPUT',
title: 'One or more input values in the request were bad',
detail: 'The object is not the correct type.'
}
]
Whereas I'm passing 'projectId', 'internalTokenClient', 'internalToken' correctly, as same for creating first version of file. Is there any issue in my payload?
I've also tried it in postman, gives same error. I've added postman screenshot for reference.
I was passing "folderId" in relationships instead of relationship lineage.
Tried passing lineage from previous version response worked properly.
const body: CreateVersion = {
jsonapi: { version: "1.0" },
data: {
type: "versions",
attributes: {
name: fileName,
extension: {
type: versions:autodesk.bim360:File"
version: "1.0",
schema: {
href: ""
}
}
},
relationships: {
item: {
data: {
type: "items",
id: <relationship lineage from previous version>
}
},
storage: {
data: {
type: "objects",
id: objectId
}
}
}
}
}
const version = await new VersionsApi().postVersion(projectId, body, internalTokenClient, internalToken)

Return object with updated association in Sequelize

Using Sequelize and MySQL, updating an object including the associated object it has. Everything updates fine but I can't get the new associated object to return. If I do another GET request it's the new one but I need it to come back on the response after an update.
I'm trying to just reload that contact object before return.
The object looks like this:
{
"id": 1,
"details": "some task details",
"contact": { //associated object
"associatedId": 1,
"name": "Mike",
}
}
This is what I'm trying
db.task.findOne({
where: {
id: taskId,
userId: req.user.get('id')
},
include: [db.contact]
}).then(
function(task) {
if (task) {
return task.update(attributes);
} else {
res.status(404).send();
}
},
function() {
res.status(500).send();
}
).then(
function(task) {
if(task) {
res.json(task);
}
},
function(e) {
res.status(400).json(e);
}
);
All you need to do is returning: true :
return task.update(attributes,{
returning: true,
plain: true
});

Stuck to figure out how to access a certain field to update

I have this JSON FILE
{
"_id": "GgCRguT8Ky8e4zxqF",
"services": {
"emails": [
{
"address": "Abunae#naa.com",
"verified": false,
"verifiedMail": "Toto#hotmail.com"
}
],
"profile": {
"name": "Janis"
},
"pushIds": []
}
I want to update my verifiedMail field but couldn't figure out how to do it in Meteor, it's always returning me an error
let VerifiedEmail = "Exemple1"
await Meteor.users.update({ _id: user._id }, { $set: { 'emails.verifiedEmail': emailRefactor} }, { upsert: true })
Couldn't figure out how to access the emails.verifiedEmail field
Tried this exemlpe worked like a charm
let VerifiedEmail = "Exemple1"
await Meteor.users.update({ _id: user._id }, { $set: { 'profile.name': emailRefactor} }, { upsert: true })
but couldn't figure out how to access emails.verifiedEmail .
Could you please help me ?
Emails is an array, while profile is an object. You have to access the first object of the email array instead
This updates the exact email address from emails
Meteor.users.update({
"emails.address": emailRefactor
}, {
$set: {
"emails.$.verified": true
}
});
Or update the first element
Meteor.users.update({
_id: user._id,
"emails.address": emailRefactor
}, {
$set: {
"emails.0.verified": true
}
});
You're trying to set verifiedEmail while the actual field is verifiedMail.

Mongoose: TypeError: Method Uint8Array.length called on incompatible receiver

I'm making a small website with i18n. When starting I used local json files, but after switching to mongodb I experience an error I don't understand. A good explanation is highly appreciated.
The error I get is this:
TypeError: Method Uint8Array.length called on incompatible receiver [object Object]
I structure i18n data with "da" and "en" properties. I then use a method for filtering relevant language.
Here are examples of my data, both json and corresponding mongoose result. Both copied from terminal (printed with console.log):
json
[ { lang: { da: 'Dansk', en: 'Danish' }, rating: 5 },
{ lang: { da: 'Engelsk', en: 'English' }, rating: 5 },
{ lang: { da: 'Tysk', en: 'German' }, rating: 5 } ]
mongoose
[ { _id: 57e2561369e4bc0a8ca6c630,
lang: { da: 'Dansk', en: 'Danish' },
rating: 5,
id: '57e2561369e4bc0a8ca6c630' },
{ _id: 57e2561369e4bc0a8ca6c631,
lang: { da: 'Engelsk', en: 'English' },
rating: 5,
id: '57e2561369e4bc0a8ca6c631' },
{ _id: 57e2561369e4bc0a8ca6c632,
lang: { da: 'Tysk', en: 'German' },
rating: 5,
id: '57e2561369e4bc0a8ca6c632' } ]
filterLanguage method
var traverse = require('traverse');
var filterLanguage = function(language, obj) {
return traverse(obj).map(function (item) {
if (this.key === language) {
this.parent.update(item);
}
});
};
So filterLanguage('da', languages); should return something like:
[ { lang: 'Dansk', rating: 5 },
{ lang: 'Engelsk', rating: 5 },
{ lang: 'Tysk', rating: 5 } ]
filterLanguage() works on a local, valid JSON file but not on the Mongoose result set...
I tried JSON.stringify(obj). I also tried setting toObject() method in Mongoose models but no luck.
What am I doing wrong and how can I fix it?
--------- EDIT ---------
Model
var mongoose = require('mongoose');
var languageSchema = mongoose.Schema({
local: {
lang: { da: String, en: String },
rating: Number
}
});
module.exports = mongoose.model('Language', languageSchema);
Query
language.find({}, function(err, results) {
var obj = filterLanguage(lang, results.languages);
console.log(obj);
});
I tried setting toObject on the schema like this:
languageSchema.set('toObject', { virtuals: true });
The error you get is related to traverse not being able to handle ObjectId instances in your results array.
Since it looks like you don't use _id or id, the easiest way to fix this is to exclude those properties from the result documents:
language.find({}, '-_id -id', function(err, results) { ... })
(-id is probably superfluous, because it's a virtual that depends on _id)

Elasticsearch: how can i create mapping before upload json data into Elasticsearch

I'm trying to create mapping before uploading json data into elasticsearch.
I don't know how to implement mapping before uploading json data in sails.js
This is my bulkupload snippet
var body = [];
//row is json data
rows.forEach(function(row, id) {
body.push({ index: { _index: 'testindex', _type: 'testtype', _id: (id+1) } });
body.push(row);
})
client.bulk({
body: body
}, function (err, resp) {
if (err)
{
console.log(err);
return;
}
else
{
console.log("All Is Well");
}
});
I want to create mapping before data upload.can any one know how to create mapping in sails.
my Json object
[ { Name: 'paranthn', Age: '43', Address: 'trichy' },
{ Name: 'Arthick', Age: '23', Address: 'trichy' },
{ Name: 'vel', Age: '24', Address: 'trichy' } ]
Before making your client.bulk() call you first need to make another client.indices.putMapping() call like this in order to save the correct mapping for the data you're about to send via the bulk call:
client.indices.putMapping({
"index": "testindex",
"type": "testtype",
"body": {
"testtype": {
"properties": {
"your_int_field": {
"type": "integer"
},
"your_string_field": {
"type": "string"
},
"your_double_field": {
"type": "double"
},
// your other fields
}
}
}
}, function (err, response) {
// from this point on, if you don't get any error, you may call bulk.
});
Remember that all these calls are asynchronous, so be careful to only call bulk once putMapping has returned successfully.
Sounds like you need PutMapping.