In business development, there may be a judgment whether there is a certain field in Mongo, and the existence of this field can be taken out of the data, you can use the following skills:
Suppose mongo’s user_info table stores three items of data as follows:
{
uid:1,
sex:1,
},
{
uid:2,
sex:0,
resume:{
name:"test1",
desc:"this is test1"
}
},
{
uid:3,
sex:1,
resume:{
name:"test1",
desc:"this is test1"}}Copy the code
Suppose our requirements are: Select ‘resume.name’ from ‘user_info’ where ‘resume.name’ does not exist. If it exists, the data is ideal. The query method is as follows:
db.user_info.aggregate([
{
$match: {$or: [{"resume.name": {$regex:""}}}}]Copy the code
If the resume.name field does not match the resume.name field, the data will not be returned