Update a document numeric field to value 0 not working [duplicate] - mongodb

This question already has answers here:
How to update Mongodb fields with omitempty flag in Golang structure
(2 answers)
Closed 2 years ago.
I have a Go function that updates a MongoDB document with a new value and it works with any value but 0. And I don't understand why. It returns a ModifiedCount of 0.
So for example, I can update the field "price" from 75 to 20. But it won't update it from 75 to 0. The important parts look like this:
type Car struct {
ID primitive.ObjectID `bson:"_id,omitempty" `
Price float64 `json:"price" form:"price" bson:"price,omitempty"`
}
...
updateResult, err := c.UpdateOne(ctx, filter, bson.M{"$set": update}, options)
where:
filter:
{ObjectID("5f1aa6da68ac05d7863e9b41")}
and update:
{ObjectID("5f1aa6da68ac05d7863e9b41") 0}

I'm not quite sure, but I think it's due to the omitempty rule on your bson, since nil can't be a valid value for float, the 0 value omit your field. More explanation here. If that field can be set as null value, I recommend you use this approach.

Related

Find by another field in mongoDB [duplicate]

This question already has answers here:
MongoDb query condition on comparing 2 fields
(4 answers)
MongoDB : querying documents with two equal fields, $match and $eq
(2 answers)
Closed 1 year ago.
how can i in mongoDB check field by another field like that in sql:
SELECT `name`,`surname` FROM `users` where `name`=`surname`
for now i try :
Credentials.findOne({ usersLen: { $lte: '$usersMaxLen' } });
^^^ - here i want access field usersMaxLen from collection
but have error:
CastError: Cast to number failed for value "$usersMaxLen" (type string) at path "usersLen" for model "Credentials"

t-sql Where = null meaning [duplicate]

This question already has answers here:
Is there any difference between IS NULL and =NULL
(3 answers)
Closed 2 years ago.
I know this is right:
select * from GoodmanLeads where Lastname is null
I know this is wrong:
select * from GoodmanLeads where Lastname = null
But it doesn't complain. Doesn't give a syntax error. Just doesn't produce any rows.
Does it have some meaning in t-sql that maybe many people don't know about?
null is kind of undefined/unknown value, so you can't really compare undefined to undefined. IS in this case will handle this specially, where as the = will be treated like a regular equality comparison.
Null is something unknown, not 0. so, something unknown is never equal to something else unknown value. simply Null is not equal to Null. so that the Lastname = null returns false always

_id : replace ObjectId to hexa value or string and indexing it

I have a collection with hexa address like this :
address: "dbaf14e1c476e76ea05a8b71921a46d6b06f0a950f17c5f9f1a03b8fae467f10"
They are all unique, so I thought I could store them in the _id field.
But I'm asking myself 2 question :
First, is there a better way to store such hexa value ? Or a String is the best option ?
And, if I set my _id field as a String, how is the indexing going to happen ? I know we can't change the index type of an _id field but is the default index type (exemple: db.collection.createIndex({address: 1}) ) working on string ?
I used to do
db.collection.createIndex({address: "text"})

How do I check if a column is NULL using rust-postgres? [duplicate]

This question already has an answer here:
How to handle an optional value returned by a query using the postgres crate?
(1 answer)
Closed 5 years ago.
I am using the rust-postgres library and I want to do a SELECT and check if the first column of the first row is NULL or not.
This is how I get my data:
let result = connection.query(
r#"
SELECT structure::TEXT
FROM sentence
WHERE id = $1
"#,
&[&uuid]
);
let rows = result.expect("problem while getting sentence");
let row = rows
.iter()
.next() // there's only 1 result
.expect("0 results, expected one...");
The only simple way I found to figure it out is the following code:
match row.get_opt(0) {
Some(Ok(data)) => some data found,
Some(Err(_)) => the column is null,
None => out of bound column index
}
Unfortunately, it seems that Some(Err(_)) is the executed path for any kind of SQL/database error, and not only if the retrieved column is NULL.
Which condition should I use to check that the column is NULL ?
If all you need to know is whether the column is NULL, you could try changing your query to:
SELECT COUNT(1) FROM sentence WHERE id = $1 AND structure IS NOT NULL
with or without the NOT.
If you want to make the logic simpler so any error is an actual error, I'd consider changing the select value to something like:
COALESCE( structure::TEXT, ''::TEXT ) AS "structure"
so it should never be NULL. That should work as long as an empty string isn't a valid non-NULL value for that column.
Otherwise, I may have misunderstood your problem.

Finding documents in mongodb collection where a field is equal to given integer value

I would like to find all documents where the field property_id is equal to -549. I tried:
db.getCollection('CollectionName').find({'property_id' : -549 })
This returns no records with a message: "Fetched 0 record(s)."
But I am seeing the document right there where the field property_id is -549.
I am not sure what I am doing wrong. The type of field is int32.
Check if there is a space in the field name 'property_id':
Try
db.getCollection('CollectionName').find({'\uFEFFproperty_id' : -549 })