How To Convert String To Double While Using Aggregation in mongoDB v3.6.5 - mongodb-3.6

I want to solve this with Aggregation if in other way possible please do let me know
I Tried This with aggregation :
db.CLV_MATERIAL_AGING.aggregate([ { $match: {"Material_Number" :
"000000000810000010"}},
{
$group: {
_id: 'null',
total:{
$sum:{$convert: {'input': '$MR', 'to': 'double'}}
},
count:{
$sum:1
}
}
}])
Give Me Error :-
{
"message" : "Unrecognized expression '$convert'",
"stack" : "MongoError: Unrecognized expression '$convert'" +
"at queryCallback
(/tmp/.mount_nosqlbn5xIBs/app/resources/app.asar/node_modules/mongodb-
core/lib/cursor.js:247:25)" +
"at
/tmp/.mount_nosqlbn5xIBs/app/resources/app.asar/node_modules/mongodb-
core/lib/connection/pool.js:531:18" +
"at _combinedTickCallback (internal/process/next_tick.js:131:7)" +
"at process._tickCallback (internal/process/next_tick.js:180:9)",
"name" : "MongoError",
"ok" : 0,
"errmsg" : "Unrecognized expression '$convert'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"
}
AND $toDouble() Not in mongoDB v3.6.5 i already Tried on mongodb v4 it worked but not in mongodb v3.5.6

Related

Wildcard search in Elasticsearch - Python

My code :
search_parm ={
"query": {
"match" : { "message" : "*error*" }
}
res = es.search(index='indice_1', body=search_parm)
result = res['hits']['hits']
print(result)
I am trying to get the messages which has the text 'error' in it.
The search parameter fetches the below result:
'message': 'Error handling attributes invalid syntax (<unknown>, line 1)'
But, the below value is not fetched :
'message': 'Sent message on queue: documentprocessing_error'
Mapping of message field :
"message" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
Please let me know how I have to edit the search parameter. Thanks.
Modified my search parameter to
search_parm1 = {"query": {"wildcard": {"message": { "value": "*error*","boost": 1.0,"rewrite": "constant_score"}}}}
Working as expected. Thanks.

NodeJS & MongoDB Canonicalize query

Having the following doc:
{
"_id" : ObjectId("5cdc08d8cd0baf815c4be240"),
"id" : "9301870",
"titles" : {
"en_US" : "en",
"de_de" : "de"
}
}
The following query:
db.getCollection('table').find({id:"9301870"},{titles:{en_US:1}})
Return the following error:
Error: error: {
"$err" : "Can't canonicalize query: BadValue Unsupported projection option: titles: { en_US: 1.0 }",
"code" : 17287
}
What do i missing here?
P.S the document has much more complex data structure - but I would like to be able to return only the relevant lang
You should use the dot notation instead of nested object, try:
db.getCollection('table').find({id:"9301870"},{ "titles.en_US":1})

MongoDB + NodeJS: Document failed validation & Data Types behaviour

I am new to MongoDB and NodeJS,
When i try to create the JsonSchema with data types, string, integer, date and bool, it is created but always throwing an error as document validation error while inserting the data, So i changed the bsonType of one data type to number, then it started creating collection records, but the observation is it is storing as Double datatype, I read somewhere in the stackoverflow, that it stores like that only, but my question is why is this behavior? WHY THE ERROR IS NOT BEING THROWN AT THE TIME OF CREATION OF THE JSONSCHEMA but it is throwing at the time of data insertion?
Also, if we have nested objects let us say, Customer object with Address as nested object, the main object's int/number values are stored as Double where as inside the address object's pincode storing as Int32. This is also very confusing. what is the difference between these objects but the structure of the schema is same.
What are the other ways to implement and having proper validated schema for MongoDB.
>
db.getCollectionInfos({name:"companysInt1s1"})
[
{
"name" : "companysInt1s1",
"type" : "collection",
"options" : {
"validator" : {
"$jsonSchema" : {
"bsonType" : "object",
"required" : [
"tin"
],
"properties" : {
"tin" : {
"bsonType" : "int",
"minLength" : 2,
"maxLength" : 11,
"description" : "must be a string and is not required, should be 11 characters length"
}
}
}
}
},
"info" : {
"readOnly" : false,
"uuid" : UUID("27cba650-7bd3-4930-8d3e-7e6cbbf517db")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "invoice.companysInt1s1"
}
}
]
> db.companysInt1s1.insertOne({tin:22222})
2019-02-14T15:04:28.712+0530 E QUERY [js] WriteError: Document failed validation :
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("5c653624e382c2ec16c16893"),
"tin" : 22222
}
})
WriteError#src/mongo/shell/bulk_api.js:461:48
Bulk/mergeBatchResults#src/mongo/shell/bulk_api.js:841:49
Bulk/executeBatch#src/mongo/shell/bulk_api.js:906:13
Bulk/this.execute#src/mongo/shell/bulk_api.js:1150:21
DBCollection.prototype.insertOne#src/mongo/shell/crud_api.js:252:9
#(shell):1:1
Am i missing something or any other documentation should i be following? Appreciate your guidance...
You need to insert as NumberInt.
when you run this
db.companysInt1s1.insertOne({tin:22222})
you are actually inserting tin as float.
so the correct way to do it is
db.companysInt1s1.insertOne({tin: NumberInt(22222) })

How to merge multiple fields in a collection?

Example entry:
{ "_id" : "00-01#mail.ru", " pass" : 123654, "field2" : 235689, "field3" : "cccp123654", "field4" : "lhfrjy" }
Desired result:
{ "_id" : "00-01#mail.ru", " pass" : 123654, 235689, "cccp123654", "lhfrjy" }
I want to have two final fields (_id and pass).
I have attempted the following:
db.emails.aggregate([
{ "$project": {
"pass": { "$setUnion": [ "$field2", "$field3" ] }
}}
])
However, this results in the following error:
2018-01-22T03:01:26.074+0000 E QUERY [thread1] Error: command failed: {
"ok" : 0,
"errmsg" : "All operands of $setUnion must be arrays. One argument is of type: string",
"code" : 17043,
"codeName" : "Location17043"
} : aggregate failed :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
doassert#src/mongo/shell/assert.js:16:14
assert.commandWorked#src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate#src/mongo/shell/collection.js:1319:5
#(shell):1:1
Can someone assist?
we can convert $objectToArray and $slice after 1 element in array
> db.io.aggregate(
[
{$addFields : {arr : {$objectToArray : "$$ROOT"}}},
{$project : { pass : {$slice : ["$arr.v", 1, 20 ] }}}
]
).pretty()
result
{
"_id" : "00-01#mail.ru",
"pass" : [
123654,
235689,
"cccp123654",
"lhfrjy"
]
}
>

MongoDB: Not enough nodes match write concern mode

Following mongodb insert operation gives an error:
db.dumpset.insert( { id: "xyz", status: "A" }, { writeConcern: { w: "activeNodes" } } )
WriteResult({
"nInserted" : 1,
"writeConcernError" : {
"code" : 100,
"errmsg" : "Not enough nodes match write concern mode \"activeNodes\""
}
})
There are 3 nodes in replica set tagged with production and the custom writeConcern looks like below:
"getLastErrorModes" : {
"activeNodes" : {
"production" : 3
}
}
MongoDB version 3.2.11, is there anything missing?

Resources